[英]angular call callback after update two way binding values in component
myApp.component('example', {
template: '<button type="button" ng-click="$ctrl.click()">click me</button>',
bindings: { value: '=', callback: '&' },
controller: function () {
this.click = function () {
this.value = 'clicked';
this.callback();
}.bind(this);
},
});
myApp.component('useExample', {
template: '<example value="$ctrl.value" callback="$ctrl.callback()"></example>',
controller: function () {
this.callback = function () { alert(this.value); }.bind(this);
},
});
Here are two components, while the second one use the first one. 这里有两个组件,而第二个组件则使用第一个组件。
The first component change this.value
and then call callback
. 第一个组件更改this.value
,然后调用callback
。 But when the second one alert(this.value)
, it got empty value instead of 'clicked'
first time. 但是,当第二个alert(this.value)
,它得到的是空值,而不是第一次'clicked'
。 It seems that the this.value
in useExample
did not be updated when the callback is triggered. 似乎在触发回调时未更新useExample
中的this.value
。
I want get new value instead of old one. 我想要获得新的价值,而不是旧的价值。
I have attempted to change this.callback()
in example
to something like $timeout(function () { this.callback(); }.bind(this), 0)
, and it works. 我曾试图改变this.callback()
的example
,以类似$timeout(function () { this.callback(); }.bind(this), 0)
和它的作品。 But I think there should be some better way to do so. 但我认为应该有一些更好的方法。
So, my question is what is the best way I should do to make useExample
read new this.value
in the callback. 因此,我的问题是使useExample
在回调中读取new this.value
的最佳方法是什么。
-- Update 1 -- -更新1-
I would prefer not to change the given interface. 我不希望更改给定的接口。
-- Update 2 -- -更新2-
aha, i just searched out this topic: AngularJS: Parent scope is not updated in directive (with isolated scope) two way binding . 啊哈,我只是搜索了这个主题: AngularJS:父范围未在指令(带有隔离范围)中以两种方式绑定进行更新 。 It seems that this question is duplicate to that one. 这个问题似乎与那个问题重复。 and i have read posts on that question, it seems $timeout
is the best(?) way, wt*. 而且我已经阅读了有关该问题的文章,看来$timeout
是最好的(?)方式,wt *。
The problem is that the watcher that binds the value from child scope to parent scope executes on a micro-thread (fiber) subsequent to the invocation of the function in the expression binding. 问题在于,将值从子作用域绑定到父作用域的观察者在调用表达式绑定中的函数之后,在微线程(纤维)上执行。
The solution is to expose the value as a local in the expression binding: 解决方案是在表达式绑定中将值公开为局部值:
myApp.component('example', {
template: '<button type="button" ng-click="$ctrl.click()">click me</button>',
bindings: {
callback: '&'
},
controller: function () {
this.click = () => {
this.value = 'clicked';
//EXPOSE this.value as $value
this.callback({$value: this.value});
};
},
});
In the above example, the value is exposed as $value
. 在上面的示例中,该值显示为$value
。
Use the exposed value as an argument of callback function: 使用暴露的值作为回调函数的参数:
myApp.component('useExample', {
template: '<example callback="$ctrl.useCallback($value)"></example>',
controller: function () {
this.useCallback = (v) => { alert(v); };
},
});
Because the value is provided as an argument of the callback, the value is immediately available. 由于该值是作为回调的参数提供的,因此该值立即可用。
The DEMO on JSFiddle . JSFiddle上的DEMO 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.