javascript-在AngularJS中深入$watch一个集合
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript-在AngularJS中深入$watch一个集合,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2888字,纯文字阅读大概需要5分钟。
内容图文
这个问题已经在这里有了答案: > How to deep watch an array in angularjs? 10个
这是我的对象:
$scope.steps = {
service: {
selected_service: '',
selected_month: '',
selected_year: '',
selected_day: ''
},
payment: {
selected_dd_type: '',
cc: {
selected_card_type: '',
credit_card_number: '',
name_on_card: '',
expiry_month: '',
expiry_year: ''
},
bank_acc: {
bsb_number: '',
account_number: '',
account_holder_name: ''
}
},
agreement: {
agreement_acceptance: false
}
}
这是$watchCollection:
$scope.$watchCollection('steps.service', function(newVal, oldVal) {
$scope.canExit = true;
});
$scope.$watchCollection('steps.payment', function(newVal, oldVal) {
$scope.canExit = true;
}, true);
$scope.$watchCollection('steps.payment.cc', function(newVal, oldVal) {
$scope.canExit = true;
}, true);
$scope.$watchCollection('steps.payment.bank_acc', function(newVal, oldVal) {
$scope.canExit = true;
}, true);
$scope.$watchCollection('steps.agreement', function(newVal, oldVal) {
$scope.canExit = true;
}, true);
一切正常.
必须有更好的方法来监视$scope.steps对象.
如果我$watch步骤,它将无法正常工作.我想Angular看的不够深
$scope.$watchCollection('steps', function(newVal, oldVal) {
$scope.canExit = true;
}, true);
感谢您的指导.
解决方法:
干得好.将值true传递为$watch中的第三个选项:
$scope.$watch('steps', function(newValue, oldValue) {
// callback on deep watch
$scope.canExit = true;
}, true);
另外,请确保在手表内部添加if条件,因为当您将值分配给$scope.steps时,手表也会被触发.
工作示例:
var app = angular.module("sa", []);
app.controller("FooController", function($scope) {
$scope.canExit = false;
$scope.steps = {
service: {
selected_service: '',
selected_month: '',
selected_year: '',
selected_day: ''
},
payment: {
selected_dd_type: '',
cc: {
selected_card_type: '',
credit_card_number: '',
name_on_card: '',
expiry_month: '',
expiry_year: ''
},
bank_acc: {
bsb_number: '',
account_number: '',
account_holder_name: ''
}
},
agreement: {
agreement_acceptance: false
}
};
$scope.reset = function() {
$scope.canExit = false;
}
// Using Math.random() just for demo so that the watch can be invoked
$scope.changeService = function() {
$scope.steps.service.selected_service = Math.random();
}
$scope.changeDate = function() {
$scope.steps.payment.cc.selected_card_type = Math.random();
}
$scope.changeAgreement = function() {
$scope.steps.agreement.agreement_acceptance = Math.random();
}
$scope.$watch('steps', function(newValue, oldValue) {
if (newValue && (newValue !== oldValue)) {
// callback on deep watch
$scope.canExit = true;
}
}, true);
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="sa" ng-controller="FooController">
<button ng-click="changeService()">Change service</button>
<button ng-click="changeDate()">Change expiry date</button>
<button ng-click="changeAgreement()">Change agreement</button>
<br>
<br>
<br>Can exit: {{canExit}}
<button ng-click="reset()">Reset</button>
</div>
内容总结
以上是互联网集市为您收集整理的javascript-在AngularJS中深入$watch一个集合全部内容,希望文章能够帮你解决javascript-在AngularJS中深入$watch一个集合所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。