我需要一个事件来捕获Angular 8中组件的重新渲染。
ngOnChanges
。ngOnChanges()
(OnChanges)
,因此您可以在输入更改时运行自定义代码。ngDoCheck()
(DoCheck)
,因此您可以实现自定义更改检测操作。 ngOnChanges(changes: SimpleChanges) {
// changes.prop contains the old and the new value...
console.log('on change', changes)
}
BehaviorSubject
。请查看以下示例, private test = new BehaviorSubject<any>(null);
getTest() {
// ...
return this.http.get(_url, options)
.map(response => {
const responseAsObject = response.json();
this.test.next(responseAsObject); // change the value!
});
}
export class TestComponent implements OnInit{
private testObj : any;
constructor(
private testService: TestService,
) {}
ngOnInit() {
this.testService.getTest
.subscribe(test => {
// here is the test object from the service, you can do whatever you want with it
// it fires when you get the updated response
});
}
}