【问题标题】:angularjs ng-repeat and ng-model does not go well with textarea/input tagangularjs ng-repeat 和 ng-model 不适用于 textarea/input 标签
【发布时间】:2014-01-10 02:42:35
【问题描述】:

当我使用 ng-repeat 生成输入标签并在自定义指令中分配 ng-model 它在每次击键时调用指令。

这是演示

http://plnkr.co/edit/Oku8EH?p=preview

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>

这很奇怪。

【问题讨论】:

    标签: angularjs angularjs-directive angularjs-ng-repeat


    【解决方案1】:

    这是因为每次您更改 arr 数组时,ng-repeat 都会重新评估它,因为它必须在观察它(请参阅 Github 上 ngRepeat 源中的 l256 here 行)

    如果将模型指向另一个数组,一切正常。

    你可以通过这个Plunkr查看它。

    【讨论】:

    • 还可能想查看:stackoverflow.com/questions/13651578/… 这解释了如何拥有一个带有大型阵列的中继器,而您不想观看每个项目。
    • 我试过str in arr track by $index,消息没有发生。有什么不同?
    猜你喜欢
    • 2017-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-20
    • 2014-09-01
    相关资源
    最近更新 更多