mic*_*ael 3 angular2-services angular
我想要的以后做什么Observable/ subscribe完成.我正在改变方法中的Input属性subscribe( onNext ),但从ngOnChanges不触发.
我应该做些什么呢?
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");
}
}
Run Code Online (Sandbox Code Playgroud)
这是"按设计"
ngOnChanges()仅在更改检测更新绑定到的时调用@Input().如果从某个地方强制改变输入,则不会调用它.
只需names为每次更新属性时执行的代码创建一个getter/setter.
| 归档时间: |
|
| 查看次数: |
5655 次 |
| 最近记录: |