AngularJS:不允许引用其他函数的“参数”

时间:2016-07-14 08:33:02

标签: angularjs service

我只是AngularJS的初学者而且我有点陷入困境。 我已经定义了一项服务:

'use strict';
var piradsApp = angular.module('piradsApp');
piradsApp.factory('dataService', function(){
  return {
    data: {
    arguments: [
       {
        name: 'T2 Weighted Images',
        entry: [{
           sector: '',
           plane: '',
           laterality: '',
           score: 0,
           comment: ''
        }]
      },
    {...} //it's an array with 4 elements, so didn't copy paste everything
   ]}
  }
});

所以服务是一个包含4个元素的数组,每个元素包含2个字段('name'和'entry')。 'entry'字段包含5个字段('sector','plane','laterality','score'和'comment')

现在我想为4个元素中的1个添加一个新条目,我尝试按如下方式进行:

var piradsApp = angular.module('piradsApp');
piradsApp.controller('ReportController',
function ReportController($scope, dataService){
   $scope.data = dataService.data;

   $scope.addRow = function(argindex){
     dataService.data.arguments[argindex].entry.push({
        sector: '',
        plane: '',
        laterality: '',
        score: 0,
        comment: ''
   })
 };
});

但我收到一条错误,指出:     Referencing 'arguments' of other function are not allowed

我在网上查了一下,找不到任何相关信息。 谁能告诉我为什么会发生这种情况以及如何摆脱它?

提前致谢

1 个答案:

答案 0 :(得分:0)

您好在添加行检查时发送数组索引$ scope.addRow

<强> HTML

<button ng-click=addRow(0)> add row</button>

<强> JS

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope , dataService) {
  $scope.name = 'World';
  $scope.data = dataService.data;
  console.log($scope.data)

   $scope.addRow = function(argindex){
     dataService.data.arguments[argindex].entry.push({
        sector: 'ghjghjgj',
        plane: 'ghjgjh',
        laterality: 'ghjgjgjgj',
        score: 0,
        comment: ''
   })
   console.log(dataService.data.arguments)
   }
});
app.factory('dataService', function(){
  return {
    data: {
    arguments: [
       {
        name: 'T2 Weighted Images',
        entry: [{
           sector: '',
           plane: '',
           laterality: '',
           score: 0,
           comment: ''
        }]
      }
   ]}
  }
  })

enter image description here

并参考https://plnkr.co/edit/FvpxG1U18qkW3Ujy5LEn?p=preview