Skip to content

Instantly share code, notes, and snippets.

@debugmodedotnet
Created September 6, 2019 09:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save debugmodedotnet/3b0c52a08c7b899d5f75ad981412f78f to your computer and use it in GitHub Desktop.
Save debugmodedotnet/3b0c52a08c7b899d5f75ad981412f78f to your computer and use it in GitHub Desktop.
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);
}
}
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
})
}
}
<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