How to $watch changes on models created by ng-repeat?

后端 未结 6 1977
感动是毒
感动是毒 2020-12-08 00:53

Consider this Plnkr for example. I don\'t know how many members of fooCollection will be created beforehand. So I don\'t know how many bar models a

6条回答
  •  隐瞒了意图╮
    2020-12-08 01:34

    You can also create a custom directive that will tell your main controller for the changes

    YourModule.directive("batchWatch",[function(){
    return {
        scope:"=",
        replace:false,
        link:function($scope,$element,$attrs,Controller){
            $scope.$watch('h',function(newVal,oldVal){
                if(newVal !== oldVal){
                  Controller.updateChange(newVal,oldVal,$scope.$parent.$index);
                }
    
            },true);
    
        },
        controller:"yourController"
    };
    }]);
    

    assume your markup is like this

    and this is your controller

    YourModule.controller("yourController",[$scope,function($scope){
        this.updateChange = function(newVal,oldVal,indexChanged){
          console.log("Details about the change");
        }
    }]);
    

    You can also play around the value provided by the directive link function which sits on first 3 arguments, scope,element and attr.

提交回复
热议问题