2016-08-20 56 views
0

以下是我的HTML代码。如何在angularjs中动态创建ng-model

<div class="col-md-6"> 
          <div class="col-sm-12" ng-repeat='department in vm.otherDepartments'> 
          <label class="checkbox-custom-labelother_label"> 
           <div class="custom-label-div other_label">department.name</div> 
           <input type="text" name="vm.experience.custom" class="other_inputbox" ng-model="vm.experience.custom[department.id]" ng-required='!vm.experience.departments'> 
          </label> 
         </div> 
        </div> 

下面是我的控制器代码:

$scope.$watch("vm.experience.custom", function (newValue, oldValue) { 
     console.log('newValue:'); 
     console.log(newValue); 
     vm.params.validateDepartments = _.filter(newValue, function (o) { 
      console.log('OOOO:'); 
      console.log(o); 
     }); 
    }); 

所以,当我改变任何输入值$手表应该被触发。但是在这种情况下不会发生这种情况。我究竟做错了什么?

回答

0

$手表默认只是一个浅表手表。 您必须将$ watch的第三个参数设置为“true”,以便进行深入观察 - 检查对象的所有子属性。 所以你必须让它

$scope.$watch ('property', function() {}, true); 
你的情况

如果.custom本身的价值改变了$腕表功能才会被触发。