angular-changedetection

Angular interpolated value display not updating when changed in timeout

做~自己de王妃 提交于 2019-12-04 19:43:24
Angular 6.0.1. This problem is manifesting itself inside a large application, but I created a simple component to try and see what is going on. The simple component has a simple template: {{myProp}} In the ngOnInit, I set this.myProp = 'hello'; and I see the word hello on screen as I would expect. So far so good. However, if I now try to update the value of myProp in a setTimeout , it never updates the UI: this.myProp = 'hello'; setTimeout(() => { this.myProp = 'goodbye'; }, 3000); The value shown in the UI is still hello . If I inject ChangeDetectorRef in my constructor and call cdr

Angular 6 View is not updated after changing a variable within subscribe

心已入冬 提交于 2019-12-04 09:02:37
问题 Why is the view not being updated when a variable changes within a subscribe? I have this code: example.component.ts testVariable: string; ngOnInit() { this.testVariable = 'foo'; this.someService.someObservable.subscribe( () => console.log('success'), (error) => console.log('error', error), () => { this.testVariable += '-bar'; console.log('completed', this.testVariable); // prints: foo-Hello-bar } ); this.testVariable += '-Hello'; } example.component.html {{testVariable}} But the view

Angular 6 View is not updated after changing a variable within subscribe

浪尽此生 提交于 2019-12-03 01:33:52
Why is the view not being updated when a variable changes within a subscribe? I have this code: example.component.ts testVariable: string; ngOnInit() { this.testVariable = 'foo'; this.someService.someObservable.subscribe( () => console.log('success'), (error) => console.log('error', error), () => { this.testVariable += '-bar'; console.log('completed', this.testVariable); // prints: foo-Hello-bar } ); this.testVariable += '-Hello'; } example.component.html {{testVariable}} But the view displays: foo-Hello . Why won't it display: foo-Hello-bar ? If I call ChangeDetectorRef.detectChanges() within

Change default change detection strategy

五迷三道 提交于 2019-11-28 07:07:34
问题 How to set the default change detection strategy to OnPush ? Can it be set globally somehow? I want to avoid having to adding this line to every component @Component({ ... changeDetection: ChangeDetectionStrategy.OnPush, ... }) 回答1: The change detection strategy can only be defined per component or directive not globally. Using a custom decorator is discouraged because it will not be supported by the upcoming offline template compiler. 来源: https://stackoverflow.com/questions/37821998/change

After child has been initialised, operation from parent component on child DOM causes ExpressionChangedAfterItHasBeenCheckedError

痴心易碎 提交于 2019-11-27 09:53:23
I'd like to make some operation from parent component on child component after child component has been initialised. Parent: export class ParentComponent implements AfterViewInit { @ViewChild('child') childComponent: ChildComponent; ngAfterViewInit() { this.childComponent.domMethod('boo'); } } <p>parent</p> <app-child #child></app-child> Child: export class ChildComponent implements OnInit { constructor(private readonly cdr: ChangeDetectorRef,) { } public term = ''; public items; ngOnInit() { this.items = [ { name: 'foo' }, { name: 'bar' }, { name: 'baz' }, { name: 'boo' }, { name: 'zoo' }, ];

After child has been initialised, operation from parent component on child DOM causes ExpressionChangedAfterItHasBeenCheckedError

纵饮孤独 提交于 2019-11-26 14:56:42
问题 I'd like to make some operation from parent component on child component after child component has been initialised. Parent: export class ParentComponent implements AfterViewInit { @ViewChild('child') childComponent: ChildComponent; ngAfterViewInit() { this.childComponent.domMethod('boo'); } } <p>parent</p> <app-child #child></app-child> Child: export class ChildComponent implements OnInit { constructor(private readonly cdr: ChangeDetectorRef,) { } public term = ''; public items; ngOnInit() {