Triggering change detection manually in Angular

后端 未结 5 882
滥情空心
滥情空心 2020-11-21 11:47

I\'m writing an Angular component that has a property Mode(): string.

I would like to be able to set this property programmatically not in response to a

5条回答
  •  臣服心动
    2020-11-21 12:18

    I was able to update it with markForCheck()

    Import ChangeDetectorRef

    import { ChangeDetectorRef } from '@angular/core';
    

    Inject and instantiate it

    constructor(private ref: ChangeDetectorRef) { 
    }
    

    Finally mark change detection to take place

    this.ref.markForCheck();
    

    Here's an example where markForCheck() works and detectChanges() don't.

    https://plnkr.co/edit/RfJwHqEVJcMU9ku9XNE7?p=preview

    EDIT: This example doesn't portray the problem anymore :( I believe it might be running a newer Angular version where it's fixed.

    (Press STOP/RUN to run it again)

提交回复
热议问题