ngOnChanges nicht feuern, wenn das Attribut geändert durch Beobachtbare/subscrption

Ich versuche zu tun, etwas nach einem Observable/subscribe abgeschlossen. Ich bin das ändern eines Input Eigenschaft in der subscribe( onNext ) Methode, aber ngOnChanges nie ausgelöst.

Was sollte ich anders machen?

import { Component, EventEmitter, 
  OnInit, AfterViewInit, OnChanges, SimpleChanges,
  Input, Output
} from '@angular/core';

@Component({
  templateUrl: 'myTemplate.html'
    , providers: [ NamesService ]
})

export class MyPage  {
  @Input() names: any[];

  constructor( public namesSvc: NamesService) {}

  ngOnInit() {
    this.getNames$()
  }


  getNames$() : void {
    this.nameService.get().subscribe( 
      (result)=>{
       this.names = result;
       console.log(`getNames$, names=${this.names}`);

       //I could doSomething() here, but it doesn't seem like the correct place
       //doSomething()  

      }
      , error =>  this.errorMessage = <any>error
    )
  }

  ngOnChanges(changes: SimpleChanges) : void {
    //changes.prop contains the old and the new value...
    console.warn(`>>> ngOnChanges triggered`)
    if (changes["names"]) {
      console.warn(`>>> ngOnChanges, names=${changes["names"]}`)
      this.doSomething()
    }
  }

  doSomething(){
    console.log("doing something");
  }
}
InformationsquelleAutor michael | 2016-10-07
Schreibe einen Kommentar