標籤:調用 ica action count 對象 函數 apply function mod
- $scope.$watch(watchFn , watchAction , deepWatch)
其中,watchFn是帶有angular運算式或函數字串;
watchAction是一個函數或者運算式,當watchFn發生變化時調用,如果是函數,其簽名是function(newValue, oldValue, scope);
deepWatch如果是ture,則會檢查被監控對象的每一個屬性是否發生了變化。
<script type="application/javascript"> var modelMy = angular.module(‘modelMy‘,[]); modelMy.controller(‘shopList‘,[‘$scope‘,function($scope){ //scope容易被壓縮成簡短的其他字元 可以用第二種方法 $scope.unitPrice = 38; $scope.count = 3; $scope.fre = 8; $scope.total = function(){ return $scope.unitPrice * $scope.count; };/* $scope.$watch( ‘count‘ , function(value){ //監聽$watch 單個‘‘ console.log(value); })*/ $scope.$watch( $scope.total , function(value){ //監聽$watch 函數 $scope.fre = value >= 100 ? 0 : 8; }) }])</script>
<body ng-app="myApp"> <div ng-controller="MessageController"> Delayed Message: {{message}} </div> </body><script> var m1 = angular.module(‘myApp‘,[]); m1.controller(‘MessageController‘, function($scope) { $scope.getMessage = function() { setTimeout(function() { $scope.$apply(function() { // 在這應用$apply方法 $scope.message = ‘2秒後顯示該文字‘; console.log(‘message:‘ + $scope.message); }); }, 2000); } $scope.getMessage(); }); </script>
秒味課堂Angular js筆記------$scope.$watch和$scope.$apply