Jus*_*ger 21 typescript angular
在ngOnInit
组件的方法中,@ Invput值将被绑定,因此您可以检查组件上的这些属性,但似乎没有办法检查@Output事件绑定.我希望能够知道@Output是否已连接到组件上.
(使用Angular 2 beta 2和TypeScript)
import {Component, Output, EventEmitter} from 'angular2/core';
@Component({
selector: 'sample',
template: `<p>a sample</p>`
})
export class SampleComponent {
@Output() cancel = new EventEmitter();
ngOnInit() {
// would like to check and see if cancel was used
// on the element <sample (cancel)="doSomething()"></sample>
// or not <sample></sample>
}
}
Run Code Online (Sandbox Code Playgroud)
Jus*_*ger 41
与user1448982相同的方法,但不使用ObservableWrapper
它是指未通过api公开的平台代码.
(使用Angular 2 RC1和TypeScript)
注意:这仅从2.0.0-beta.16及更高版本开始工作
import {Component, Output, EventEmitter} from '@angular/core';
@Component({
selector: 'sample',
template: `<p>a sample</p>`
})
export class SampleComponent {
@Output() cancel = new EventEmitter();
private isCancelUsed = false;
ngOnInit() {
this.isCancelUsed = this.cancel.observers.length > 0;
}
}
Run Code Online (Sandbox Code Playgroud)
该ObservableWrapper.hasSubscribers
方法在内部执行此行,因此您可以在此处执行相同的操作.
当使用TypeScript时,如果Angular最终改变了EventEmitter
a Subject
(它是part Observable
,即.observers
属性),你将得到一个转换时间错误.
归档时间: |
|
查看次数: |
5518 次 |
最近记录: |