更新组件中的变量后,Angular不更新

时间:2020-07-29 01:06:29

标签: angular

我使用Angular 9并修改了我在模板中使用的变量。但是不幸的是我的模板没有更新。谁能解释一下让Angular知道组件中的某些内容发生了变化的典型方法是什么?

setGuid("*")

foo.component.html

Property 'then' does not exist on type 'boolean | Promise<boolean>'. Property 'then' does not exist on type 'false'.

2 个答案:

答案 0 :(得分:1)

您需要使用箭头功能。 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

答案 1 :(得分:0)

如果您不更改“ in memory”变量,Angular将不会检测到更改。就您而言,仅推送相同的变量将不会触发更改检测。您应该this.projects = this.projects.slice()或类似的方法来更改内存变量并触发更改。如果这不适合您,您还可以使用其他方法,例如注入ChangeDetector然后调用ChangeDetector.detectChanges();

constructor(private cd: ChangeDetectorRef) {}

...
this.projects.push('foo');
this.cd.detectChanges();
...
相关问题