我不明白为什么当我更改输入中的绑定基元时,$onChanges没有被启动.有人能看出我做错了什么,并以简单的方式解释这个问题吗?在我无法在实际应用程序中使用它之后,我做了一个
plunkr的快速测试应用程序.
angular .module('test',[]) .component('test',{ template: '<child application="vm.application"></child>',controller: 'testCtrl as vm' }) .controller('testCtrl',function() { var vm = this; vm.$onInit = function () { vm.application = { data: { name: 'Test' } } }; }) .component('child',{ template: '<input type="text" ng-model="vm.application.data.name">',bindings: { application: '<' },controller: 'childCtrl as vm' }) .controller('childCtrl',function() { var vm = this; vm.$onChanges = function (changes) { console.log('CHANGED: ',changes); }; })
解决方法
这是$onChanges而不是$onChange.
此外,onChange仅在父值更改时更新,而不是子项.看看这个plunkr.注意,只有当你输入第一个输入时,console.log才会触发.