在ngOnInit
组件的方法中,@ Input值将被绑定,因此您可以检查组件上的那些属性,但是似乎没有办法检查@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>
}
}
与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;
}
}
该ObservableWrapper.hasSubscribers
方法在内部执行此行,因此您可以在此处执行相同的操作。
当使用打字稿如果角度过最终改变你会得到一个transpile时间误差EventEmitter
从Subject
(这是一部分Observable
,因此.observers
属性)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句