Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
angularjs ng repeat和ng模型与textarea/input标记不匹配_Angularjs_Angularjs Directive_Angularjs Ng Repeat - Fatal编程技术网

angularjs ng repeat和ng模型与textarea/input标记不匹配

angularjs ng repeat和ng模型与textarea/input标记不匹配,angularjs,angularjs-directive,angularjs-ng-repeat,Angularjs,Angularjs Directive,Angularjs Ng Repeat,当我生成带有ng的输入标记时,在自定义指令中重复并分配ng模型 它在每个按键上都调用指令 这是演示 var-app=angular.module('myApp',[]); 应用程序控制器('MyCtrl',函数($scope){ $scope.arr=[“1234567”]; }); app.directive('myDirective',函数($compile,$timeout){ var num=0; 返回{ 链接:功能(范围、el、属性){ log('当ng模型作为arr[$index]

当我生成带有ng的输入标记时,在自定义指令中重复并分配ng模型 它在每个按键上都调用指令

这是演示

var-app=angular.module('myApp',[]);
应用程序控制器('MyCtrl',函数($scope){
$scope.arr=[“1234567”];
});
app.directive('myDirective',函数($compile,$timeout){
var num=0;
返回{
链接:功能(范围、el、属性){
log('当ng模型作为arr[$index]给出时,textarea中的每个keyup事件都会发生这种情况,为什么?');
}
};
});
arr[0]:{{arr[0]}}

这很奇怪。

这是因为每次更改
arr
数组时,
ng repeat
都在重新评估,因为它必须在监视它(请参阅Github上ngRepeat sources中的l256行)

如果将模型指向另一个阵列,则一切正常


你可以用这个来检查。

也可能想检查:这解释了如何让中继器有一个大的阵列,你不想看每一个项目。我在arr track by$index中尝试了
str,消息没有发生。这有什么不同?
var app = angular.module('myApp', []);
app.controller('MyCtrl', function($scope) {
  $scope.arr = ["1234567"];
});

app.directive('myDirective', function($compile, $timeout) {
  var num=0;
  return {
    link: function(scope, el, attrs) {
      console.log('this happens with every keyup event in textarea when ng-model is given as arr[$index], why?');
    }
  };
});


<body ng-app="myApp" ng-controller="MyCtrl">
  arr[0] : {{arr[0]}} <br/>
  <input my-directive ng-repeat="str in arr" ng-model="arr[$index]" />
  </input>
</body>