可能是因为在组件模板中没有正确地绑定数据。检查组件HTML模板中的绑定是否正确。另一种可能是没有在组件中正确地声明属性/变量。使用ngOnInit方法来确保属性/变量被正确声明和初始化。 例如,以下代码可能会导致此
export class MyComponent implements OnInit {
data: any;
constructor(private myService: MyService) {}
ngOnInit() {
this.myService.getData().subscribe(res => {
console.log(res); // This logs data correctly in console
this.data = res; // This is not being reflected in the template
});
}
}
在HTML模板中,使用{{ data }}来正确地绑定data属性:
{{ data }}