I guess everybody knows how to make angular to respond on the changes made to scope variables- to add some callback to "$watch" , something like this:
scope.$watch('name', function(newValue, oldValue) {
scope.counter = scope.counter + 1;
});
But the cool thing that $watch can listen and respond to changes of functions which return something too.
For example:
$scope.tabs=[{active:true, name:'first'},{active:false, name:'second'},{active:false, name:'third'}}];
$scope.$watch(function(){ return $scope.tabs.filter(function(t){ return t.active; })[0]; },function(tab){
$scope.$broadcast('tab-selected',{name:tab.name})
},true)
No comments:
Post a Comment