TypeError:非法调用 - 当$ watch数据包含File对象angularJS时

时间:2017-08-17 19:01:27

标签: angularjs angularjs-scope angularjs-watch

我有一个名为models.questions的对象,我使用$ watch来监视它的变化(深度监视,监视models.questions的整个对象)。就像:

$scope.$watch('models.questions', function (newVal, oldVal) {
     do sth...
}

models.questions可能包含以下数据:

{
  "Question": {
    "Description": "",
    "Index": "",
    "QuestionType": "StaticElement",
    "tag": "This is a static element",
    "originalName": "Static - Label/Link/Image",
    "Title": "",
    "Required": true,
    "Dynamic": "",
    "Dependency": "",
    "Hidden": false,
    "Options": [],
    "displayOptions": [
      "x.png"
    ]
  },
  "Dependencies": {
    "RelationBetweenOptions": "",
    "Action": "",
    "OptionsForSelect": [],
    "DependencyOptions": []
  }
}

我的代码一直运作到最近。我需要将一些文件类型对象放入"选项"。我曾经将字符串或int类型放入"选项",代码可以工作。

但是一旦我把一个File类型的对象放进去。当代码执行$ watch时,我收到错误

 angular.js:14525 TypeError: Illegal invocation
at sa (angular.js:1205)
at sa (angular.js:1189)
at sa (angular.js:1205)
at sa (angular.js:1205)
at sa (angular.js:1189)
at m.$digest (angular.js:17993)
at m.$apply (angular.js:18269)
at angular.js:20186
at e (angular.js:6274)
at angular.js:6554

文件对象是用户选择上传的文件。就像:

<md-input-container ng-if="item.Question.Required">
        <button ngf-select ngf-change="upload($files,$index)" multiple="multiple">Add+</button>
</md-input-container>

上传功能如下:

 $scope.upload = function (file, index) {
     if ($scope.models.questions[index].Question.displayOptions === undefined) {
    $scope.models.questions[index].Question.displayOptions = [];
  }

  for (var i = 0; i < file.length; i++) {
    var count = 0;
    var tmpFileName = file[i].name;
    while ($scope.models.questions[index].Question.displayOptions.indexOf(tmpFileName) !== -1) {
      tmpFileName = file[i].name + '(' + count + ')';
      count++;
    }
    $scope.models.questions[index].Question.displayOptions.push(tmpFileName);
    $scope.models.questions[index].Question.Options.push(file[i]);   
  }
};

我不知道如何调试这个。有谁知道导致此错误的原因是什么? 谢谢。

0 个答案:

没有答案