【发布时间】:2017-07-24 01:08:56
【问题描述】:
我是 AngularJS 的新手。我正在尝试构建一个用于更新当前页面的分页组件。我遇到的问题是,当组件对具有双向绑定的值进行更改时。新值不会立即提供给父级。
为了等待绑定值更新,我应该做些什么吗?或者这是一个模式问题,我应该采取不同的方法来解决问题?
组件 - paging.js
angular.module('someModule')
.component('paging', {
bindings: {
page: '=',
getNextPage: '=' // <- Side note: This is a function, I had problems using the '&' binding
},
controller: function($scope) {
var $ctrl = this;
$ctrl.nextPage = function () {
$ctrl.page++;
$ctrl.getNextPage(); // <-- Parent will still have the old value for 'page'
// THIS WOULD WORK, PARENT WOULD HAVE THE UPDATED VALUE FOR 'page'
// setTimeout(function(){
// $ctrl.action();
// }, 1000);
// COULD ALSO PASS THE VALUE THIS WAY
// $ctrl.action($ctrl.page);
}
});
父控制器
...
$scope.getNextPage = function () {
$scope.page; // <- This still has the old value
...
}
...
【问题讨论】:
标签: angularjs