【发布时间】:2021-10-24 17:33:13
【问题描述】:
我想从 app.component.html 的一部分的导航栏刷新我的卡片组,所以我准备了 refresh() 函数。
当它被调用时,它会更新变量 Cards 但不会在 mainView.html 的 html 元素上的 ngFor 中呈现它。
如果我从 mainView.html 中的 html 元素调用(如 (click)="loadCards()"),它会呈现更新的集合,但如果相同则不会( (click)="refresh()") 在 app.component.html 中完成。
export class MainView implements OnInit {
constructor(private mMainController: MainController) {}
Cards: any = [];
ngOnInit() {
this.loadCards();
}
loadCards() {
this.mMainController.getAllCards().subscribe(
(data) => {this.Cards = data); },
(error) => {},
() => {console.log(this.Cards));
}
...
}
export class AppComponent {
...
constructor(private router: Router, private mMainView: MainView) {}
refresh(){
console.log('done');
this.mMainView.loadCards();
}
...
}
更新
尝试使用 @Input() 但无法正常工作。我按照接受的答案中的说明实现了 RefreshService,现在我可以刷新其他组件的内容。
感谢大家的快速回复。
【问题讨论】:
-
我认为问题出在您调用 MainView 的方式上,这似乎不会触发摘要。我认为正确的方法是将您的卡片数组作为输入传递给您的组件
[cards]=cards,然后直接从子组件修改卡片数组。
标签: javascript angular