如何将Promise绑定到组件属性?

Dow*_*ski 8 angular

我有一个promise对象需要解析为另一个组件,如何实现这一点,以便在component-one解析promise时,解析的promise对象component-two也可以解析?

ComponentOne摘录

@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);
    });
  }
}
Run Code Online (Sandbox Code Playgroud)

ComponentTwo摘录

@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);  
  }
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*yer 8

简单的方法是将async管道添加到模板中,如下所示:

{{promisedData | async}}

编辑:这是一个显示async管道的工作示例:plunker

Edit2:Plunker显示OnInit:plunker