摘要:$watch、ng-change、ng-click的比較
$watch可以指定監控的範圍,一旦範圍內的value發生改變,所有地方都會自動更新;
$watch指令,決定監控的model內容,監控內容發生改變後,並接著執行function
<input type="text" ng-model="test1"/>
- $scope.$watch('test1', function(newValue, oldValue) {
- //這裡輸入觸發$watch之後,欲觸發的行為
- },true);
<!DOCTYPE html>
<html ng-app>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script>
<meta charset=utf-8 />
<title>$watch</title>
</head>
<body ng-controller="AddressControll">
$watch方法: <input type="text" ng-model="test1"/>
<br>
ng-change方法:<input type="text" ng-model="test2" ng-change="update()"/>
<br>
ng-click方法:<input type="text" ng-model="test3" ng-click="update()"/>
<span>{{test1}}</span>
</body>
</html>
function AddressControll($scope) {
$scope.$watch('test1', function(newValue, oldValue) {
console.log(newValue,oldValue);
},true);
$scope.update = function() {
console.log("test2");
};
REF>>http://ithelp.ithome.com.tw/question/10139851