我有一个父组件和一个子组件 . 两者都共享相同的服务 . 单击ParentComponent上的按钮可以使用新项更新SharedService中的对象数组 . 当我查看我的页面时,看起来ChildComponent实际上并没有使用新条目更新其{}的模板 .
我正在使用github.com/AngularClass/angular-starter . 我的SharedService是"appState",我在那里写了appState.state.listofitems.push({'item':'Apple'}) . 链接到appState代码:https://github.com/AngularClass/angular-starter/blob/master/src/app/app.service.ts
使用angular 1,我有$ scope . $ apply(...)有没有办法让我的子组件用新数据“刷新”?
ParentComponent===
constructor(public appState:AppState) {
this.appState.set('listofitems', [])
}
buttonClicked() {
this.appState.state.listofitems.push({'item': 'Apple'})
}
ChildComponent模板
<span>{{this.listofitems}}</span>
ChildComponent TypeScript
constructor(public appState:AppState) {
}
ngOnInit() {
this.listofitems = this.appState.get('listofitems');
}