directive 中监听 controller 多个对象或数组的变化
1 | //init |
$apply 和 $digest
什么时候用 $apply 和 $digest
1 | {{rootName}} |
1 | app.controller('testCtrl', ['$scope', '$rootScope', function($scope, $rootScope){ |
- 当使用非 angular 包装方法,如 setTimeout 等原生 js 方法去改变 model 值的时候,需手动调用 $apply 或 $digest 去更新 view 显示。
- $apply 会一直向上检查到 $rootScope 变化。$digest 只会检查自己及子 scope 变化。
- 使用 $apply 时,总是 $scope.$apply(function(){}) 这样使用,因为这样 angular 会 try… catch…