我有一个以下代码,其中我想根据$ scope值更改使用Ng-switch更改UI视图。
但是对我来说它正常工作然后是什么才能使用$ watch
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope, $interval) {
$scope.firstName = "John";
$scope.lastName = "Doe";
$scope.count = 0;
$scope.counter = function(){
$scope.count += 1;
};
$scope.$watch('count',function(){
console.log('a');
});
$interval($scope.counter,'10000');
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
{{ firstName + " " + lastName }}<br/><br/>
{{count}}
<div ng-switch="count">
<div ng-switch-when="1">div 1 called</div>
<div ng-switch-when="2">div 2 called</div>
<div ng-switch-when="3">div 3 called</div>
<div ng-switch-when="4">div 4 called</div>
<div ng-switch-when="5">div 5 called</div>
<div ng-switch-when="6">div 6 called</div>
<div ng-switch-when="7">div 7 called</div>
<div ng-switch-when="8">div 8 called</div>
</div>
</div>
答案 0 :(得分:0)
看来这里的$ watch只是用于调试......前一个开发人员必须使用console.log
检查代码是否正常工作。
答案 1 :(得分:0)
如果$ scope.count已更改,则 $ watch 块会退出。你有4件事情要做:
看来你的$ watch完全用于调试。