I have made a variation of an example app from an Angular book.
A working example (in development mode) can be found here: http://ng-ex.watchduck.net/#/21
The source code can also be found on GitHub.
The component log has the variable autoRefresh. (see here)
export class LogComponent implements OnInit {
autoRefresh: boolean = false;
constructor(private changeDetector: ChangeDetectorRef) { }
ngOnInit() {
this.changeDetector.detach(); // Here is the problem.
}
render() {
this.changeDetector.reattach();
this.changeDetector.detectChanges();
this.changeDetector.detach();
}
start() { this.autoRefresh = true; }
stop() { this.autoRefresh = false; }
}
I would like to use it in the component (in the green box) to show the current status, and to disable two buttons based on it. (see here) But that does not work. No initial value is shown, and then it just sticks with the first value.
<button (click)="start()" [disabled]="autoRefresh">start</button>
<button (click)="stop()" [disabled]="!autoRefresh">stop</button>
But when I place the buttons outside of the component it works. (see here)
<button (click)="viewer.start()" [disabled]="viewer.autoRefresh">start</button>
<button (click)="viewer.stop()" [disabled]="!viewer.autoRefresh">stop</button>
<app-log #viewer [logs]="logs"></app-log>
This is quite counter-intuitive, right? What is going wrong here?
Edit: The problem goes away, when I remove this.changeDetector.detach(); from ngOnInit.
But of course having it there is the point of this exercise.
To clarify: If the disabled attributes were removed, the start and stop button in the component would work fine. What fails is binding the variable autoRefresh to the disabled attributes (as well as using it with ngSwitch or just showing it with {{autoRefresh}}).
