Created
September 6, 2019 09:21
-
-
Save debugmodedotnet/3b0c52a08c7b899d5f75ad981412f78f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, OnInit, Input, ChangeDetectionStrategy, OnChanges, ChangeDetectorRef } from '@angular/core'; | |
import { Observable } from 'rxjs'; | |
import { TouchSequence } from 'selenium-webdriver'; | |
@Component({ | |
selector: 'app-child', | |
template: ` | |
<h2>Items : {{_data.counter}} </h2> | |
<button (click)='0'>Refresh</button> | |
`, | |
changeDetection:ChangeDetectionStrategy.OnPush | |
}) | |
export class AppChildComponent implements OnInit , OnChanges { | |
_data ; | |
@Input() data : Observable<any> ; | |
@Input() notifier : Observable<boolean>; | |
constructor(private cd : ChangeDetectorRef) { } | |
ngOnInit() { | |
this.data.subscribe(value=>{ | |
this._data = value; | |
this.cd.markForCheck(); | |
}); | |
this.cd.detach(); | |
this.notifier.subscribe(value=>{ | |
if(value){ | |
this.cd.reattach(); | |
} | |
else{ | |
this.cd.detach(); | |
} | |
}) | |
} | |
ngOnChanges(){ | |
console.log(this.data); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, OnInit, ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core'; | |
import { BehaviorSubject } from 'rxjs'; | |
@Component({ | |
selector: 'app-root', | |
templateUrl: './app.component.html', | |
styleUrls: ['./app.component.css'] | |
}) | |
export class AppComponent implements OnInit { | |
_counter = 0; | |
data: any; | |
_interval; | |
t = false; | |
notifier = new BehaviorSubject(false); | |
start(){ | |
if(!this._interval){ | |
this._interval = setInterval(()=>{ | |
this.data.next({ | |
counter: ++this._counter | |
}) | |
},10 ) | |
} | |
} | |
stop(){ | |
clearInterval(this._interval); | |
this._interval = null; | |
} | |
toggle(){ | |
this.t = !this.t; | |
this.notifier.next(this.t); | |
} | |
constructor() { | |
} | |
increment(){ | |
// this.data.counter++; | |
// this.data = { counter: ++this.data.counter + 10}; | |
} | |
ngOnInit() { | |
// this.data = { | |
// counter: 0 | |
// } | |
this.data = new BehaviorSubject({ | |
counter: 0 | |
}) | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<button (click)='start()'>Start</button> | |
<button (click)='stop()'>Stop</button> | |
<button (click)='toggle()'>Toggle Change Detection</button> | |
<hr /> | |
<app-child [notifier]='notifier' [data]='data'></app-child> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment