var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.form = {
name: 'my name',
age: '25'
}
$scope.$watch('form', function(newVal, oldVal){
console.log(newVal);
},true);
$scope.foo= function(){
$scope.form.name = 'abc';
$scope.form.age = $scope.form.age++;
}
$scope.foo();
$scope.bar= function(){
$scope.form.name = 'xyz';
$scope.form.age = $scope.form.age++;
}
$scope.bar();
});Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">{{form}}<br>
<label>Name:</label> <input type="text" ng-model="form.name"/><br>
<label>Age:</label> <input type="text" ng-model="form.age"/> </div>Run Code Online (Sandbox Code Playgroud)
在控制器中我改变了'form'对象属性四次,但是$ watch没有开火.从UI(输入),如果我改变它工作
您的情况是,我认为在所有快速更新期间,角度摘要周期没有时间通过。
以下是一些参考资料,可以帮助您更多地了解 Angular 1.X 的摘要循环:
防止此类行为;您可以使用 $timeout (具有 $apply)依赖项强制摘要通过:https://docs.angularjs.org/api/ng/service/ $timeout
例子:
$timeout(function(){
$scope.form.name = name;
$scope.form.age = $scope.form.age++;
})
Run Code Online (Sandbox Code Playgroud)
我创建了一个 jsfiddle 来说明你的情况:
http://jsfiddle.net/IgorMinar/ADukg/
| 归档时间: |
|
| 查看次数: |
143 次 |
| 最近记录: |