angular - 更新组件中的变量后角度不更新
问题描述
我使用 Angular 9 并修改了我在模板中使用的变量。但不幸的是,我的模板没有更新。谁能解释一下让 Angular 知道我的组件中的某些内容发生了变化的典型方法是什么?
projects: any[] = [];
ngAfterViewInit(): void {
this.actRoute.params.subscribe((params) => {
this.electronService.foo.bar(
.then((x) => {
var history = x.y();
history.on("begin", () => {
this.projects.push('foo');
});
history.execute();
});
});
}
foo.component.html
<ul>
<li *ngFor="let project of projects;">{{project}}</li>
</ul>
解决方案
您需要使用箭头功能。this
与您认为的含义不同
ngAfterViewInit(): void {
let projects = this.projects;
projects = [];
this.actRoute.params.subscribe((params) => {
this.electronService.foo.bar(
.then((x) => { // arrow function
const history = x.y(); // use const
history.on("begin", () => { // arrow function
projects.push('foo');
});
history.execute();
});
});
}
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
推荐阅读
- javascript - 如何使子组件的可触摸不透明度更改父组件中的状态(钩子)
- python - 我需要通过选择最接近的排序来减少节点之间的弧数
- flutter - 在未来调用小部件
功能颤振 - java - 是否可以在 java 的 CASPER 框架中使用 rest 模板(调用后)?
- reactjs - 使用 pm2 部署 prod 构建后出现“您需要启用 JavaScript 才能运行此应用程序”
- linux - Linux命令从不以数字开头的文件中过滤记录
- cucumber-junit - 如何将两个或多个 Cucumber TestRunner 文件组合成一个单独的文件并依次运行?
- fpga - 用于 5 个 fpga-shells vcu118 的 fmc 卡
- python-3.x - 我需要从“从日期”一个字段中获取日期并增加 19 天并将其传递到机器人框架中的“最新”中。还有其他方法吗
- cmake - 如果不使用有效的现代 CMake,有没有办法在 CMake 中生成错误?