I have a promise object needs to be parsed to another component, how to achieve this so that when the promise in component-one
is resolved, the promise object parsed to component-two
can also be resolved?
@Component({
selector: 'component-one',
template: `
<h1>Title</h1>
<component-two [promisedData]="promisedData"></component-two>
`,
directives: [ComponentTwo]
})
export class ComponentOne {
promisedData: Promise<any>;
constructor () {
this.promisedData = new Promised(resolve => {
setTimeout(function(){
resolve('Test');
}, 1000);
});
}
}
@Component({
selector: 'component-two',
template: `
<h2>
{{processedData}}
// the {{processedData}} is undefined becaused the
// resolved promise doesn't get to parse to the component-two.
</h2>
`,
inputs: ['promisedData']
})
export class ComponentTwo {
promisedData: Promise<any>;
processedData: string;
constructor () {
PromisedData.then(data => this.processedData = data);
}
}
The easy way is to just add the async
pipe to your template like this:
{{promisedData | async}}
Edit: here's a working example showing the async
pipe: plunker
Edit2: Plunker showing OnInit: plunker
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With