angular - 无法在Angular中读取组件的html文件中的变量
问题描述
我正在尝试显示数组的数据(填充在 中chat.component
)..
public matchesList = [];
loadMatches() {
...
if (result.success) {
this.matchesList = result.matches_list.split(',');
console.log(this.matchesList);
}
}
..在chat.component.html
..
<div class="matchesList" *ngFor="let match of matchesList">
<p>{{match}}</p>
</div>
..没有任何运气!
向console.log()
我展示了该数组已正确填充:(3) ["3", "5", "6"]
.
loadMatches()
从另一个组件调用home.component
..
import { ChatComponent } from '../chat/chat.component';
@Component({
providers: [ChatComponent],
...
})
loadChatViewData() {
this.chatComp.loadMatches();
}
<a class="nav-link" (click)="loadChatViewData()"></a>
为什么matchesList
的数据不能在 中访问chat.component.html
?
谢谢!
解决方案
发生这种情况是因为您用来调用的实例this.chatComp.loadMatches()
与用于渲染以下模板的实例不同 -
chat.component.html
<div class="matchesList" *ngFor="let match of matchesList">
<p>{{match}}</p>
</div>
providers: [ChatComponent]
与用于渲染的实例相比,您使用的实例将为您提供不同的实例。
如果您可以共享以下组件的 html/.ts 文件以及将组件放置在您的 UI 中的代码 [html],我们将能够为您提供进一步的帮助 -
1. Chat Component.ts/.html
2. Home component.ts/.html
3. The parent [if any] which holds those two components [If not then let us know how those are rendered [i.e. Parent/Child?]
推荐阅读
- webpack - 将所有css提取到一个文件中的目的是什么
- angular - 在组件类中调用服务构造函数
- php - Woocommerce - 更改快速查看按钮文本和眼睛图标
- node.js - 将 HTML 按钮链接到 node.js 帖子
- angular - 为什么我的代码在 ANGULAR 5 中出现名称属性错误?
- python - 'GroupedData' 对象在 spark 数据帧中执行枢轴时没有属性'show'
- sql - Microsoft Access 数据库引擎在运行更新查询时找不到对象“错误
- pip - 使用 pip 安装 pull git 请求
- android - 仅从实时数据库中检索一次数据
- cloud-foundry - Cloud Foundry:凭据被拒绝