jon*_*gen 10 ondestroy angular
是否可以在ngOnDestroy上发出自定义事件?我试过,但似乎它不起作用......我基本上需要知道从UI中删除指令的时间.
@Output() rowInit = new EventEmitter();
@Output() rowDestroy = new EventEmitter();
ngAfterViewInit() {
this.rowInit.emit(this);
}
ngOnDestroy() {
console.log("I get called, but not emit :(, ngAfterViewInit works :)");
this.rowDestroy.emit(this);
}
Run Code Online (Sandbox Code Playgroud)
Thi*_*ier 16
我认为您可以使用EventEmitter服务中定义的而不是组件本身.这样,您的组件将利用服务的此属性来发出事件.
import {EventEmitter} from 'angular2/core';
export class NotificationService {
onDestroyEvent: EventEmitter<string> = new EventEmitter();
constructor() {}
}
export class MyComponent implements OnDestroy {
constructor(service:NotificationService) {
this.service = service;
}
ngOnDestroy() {
this.service.onDestroyEvent.emit('component destroyed');
}
}
Run Code Online (Sandbox Code Playgroud)
其他元素/组件可以在此EventEmitter上订阅以通知:
this.service.onDestroyEvent.subscribe(data => { ... });
Run Code Online (Sandbox Code Playgroud)
希望它对你有帮助,蒂埃里
| 归档时间: |
|
| 查看次数: |
18211 次 |
| 最近记录: |