【发布时间】:2018-11-30 10:36:33
【问题描述】:
假设我有一个如下所示的组件:
parent.component.js
...
template: `<div>
<child-one value="value" callback="callback"></child-one>
<child-two value="value"></child-two>
</div>`,
controller: function() {
this.callback = function(n) {
this.value = n;
}
}
...
那么子组件是这样的:
childOne.component.js
...
bindings: {
value: '<',
callback: '<'
},
template: `<input type="text"
ng-model="$ctrl.value"
ng-change="$ctrl.callback($ctrl.value)"
/>`
...
childTwo.component.js
...
bindings: {
value: '<'
},
template: `<div>{{$ctrl.value}}</div>`
...
(绑定技术感谢krawaller)
我希望childOne 中设置的值转到childTwo。更新childOne 中的value 确实会更新parent 中的value,但不会将其传递给childTwo。
【问题讨论】:
-
最好的方式是使用服务来共享数据。
标签: angularjs data-binding binding