2016-07-26 3 views
1

나는 다음과 같이 이벤트 이미 터가있는 구성 요소가 다음 loadStuff 방법은 Promise<any> 반환Angular2 "출력"콜백

<my-component (onLoadRequired)="loadStuff()" /> 

: 같은

@Output() onLoadRequired: EventEmitter<any> = new EventEmitter<any>(); 

이 사용되는 것을.

loadStuff 약속이 언제 해결되었는지 알고 싶으시면 my-component이 필요합니다. 어떻게이 일을 성취 할 수 있습니까? 당신이 loadStuff() 돌려 Promise을하면

답변

2

이 작동합니다 : 약속이 해결 될 때 <my-component>에서

<my-component #mycomponent (onLoadRequired)="loadStuff().then(val => mycomponent.done()" /> 

done()가 호출됩니다.

(테스트하지 않음)

+0

대체 방법? HTML에서 논리가 부족한 부분을 원했고, 이상적으로는 하위 구성 요소 자체 내에서 처리하는 것이 가장 이상적이었습니다. – David

+0

'loadStuff' 메소드를''와'loadStuff(). then (...)'에 전달할 수 있습니다. 당신은'loadStuff()'의'this'가 부모 클래스에서'loadStuff =() => this.doSomething()'또는'loadStuff = this.doSomething.bind (this)'처럼 reatained되도록주의해야합니다. –

+0

'loadStuff 메소드를 전달할 수 있습니다'부분에 대해 궁금합니다. 뭔가 : @Input() loadMethod : Function;'? – David