havietduc91 / angular2.0

Research something about AngularJS 2.0
0 stars 0 forks source link

Observable vs subscribe #2

Open havietduc91 opened 7 years ago

havietduc91 commented 7 years ago

http://blog.thoughtram.io/angular/2016/02/22/angular-2-change-detection-explained.html

@Component({
  template: '{{counter}}',
  changeDetection: ChangeDetectionStrategy.OnPush
})
class CartBadgeCmp {

  @Input() addItemStream:Observable<any>;
  counter = 0;

  ngOnInit() {
    this.addItemStream.subscribe(() => {
      this.counter++; // application state changed
    })
  }
}
havietduc91 commented 7 years ago
is shorthand for

<input [ngModel]="yourVar" (ngModelChange)="yourVar=$event"></input>

(see e.g. http://victorsavkin.com/post/119943127151/angular-2-template-syntax)

You could do something like this:

<input [(ngModel)]="yourVar" (ngModelChange)="changedExtraHandler($event)"></input>