AngularJS changing model value in one controller triggers model update in others

前端 未结 2 834
野趣味
野趣味 2021-02-04 22:13

EDIT:

Ok I update the example to avoid the loop problem, so back to the original question it sill recalculate B model objects.

In this example: http://jsfiddle.n

2条回答
  •  旧巷少年郎
    2021-02-04 22:37

    here is a working solution :

    http://jsfiddle.net/m8xtA/1/

    Using $watch is a good way to accomplish that.

    function A($scope) {
        $scope.m='a';
        $scope.counter = 0;
        //executed each time `m' is changed
        $scope.$watch('m',function(){
            $scope.counter++;
        })
    }
    function B($scope) {
        $scope.m='b';
        $scope.counter = 0;
        //executed each time `m' is changed
        $scope.$watch('m',function(){
            $scope.counter++;
        })
    }    
    

    Hope this help, cheers

提交回复
热议问题