无法获得$ watch来监听对象属性更改

时间:2014-03-16 15:06:23

标签: angularjs

我将数组中的用户名设置为不同的对象,并且我试图在指令中监听名称的更改。因此,当我在文本中输入任何内容时,模型会发生变化,那么为什么名称更改时不会发生$ watch fire?

JS

var app = angular.module('plunker', []);
app.directive('mydirective', function() {
    return {
      link : function(scope, elem, attr, ctrl) {
      scope.$watch(scope.obj.users , function(newValue) {
          console.log(scope.obj)
      },true)
    } 
    } 
});
app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';
  $scope.obj = {

      "users" : [
         {
            'name' : 'user1'
         },
         {
            'name' : 'user2'
         } 
      ]

  }
});

HTML

<div ng-repeat="user in obj.users">
      <input mydirective type="text" ng-model="user.name"/>
</div>

Plnk - http://plnkr.co/edit/PKp57zhoa7kvUthqB5Zn?p=preview

0 个答案:

没有答案
相关问题