Angular2 beta8和beta9中的异步管是否已损坏?

时间:2016-03-24 02:29:31

标签: pipe angular

我在Angular2官方网站上试过这个例子:https://angular.io/docs/ts/latest/guide/pipes.html

未显示任何消息且未显示任何错误。

import {Component} from 'angular2/core';
// Initial view: "Message: "
// After 500ms: Message: You are my Hero!"
@Component({
  selector: 'hero-message',
  template: 'Message: {{delayedMessage | async}}',
})
export class HeroAsyncMessageComponent {
  delayedMessage:Promise<string> = new Promise((resolve, reject) => {
    setTimeout(() => resolve('You are my Hero!'), 500);
  });
}

以下是plunker:https://plnkr.co/edit/SokJZ8cGGx4upIaG7wW8?p=catalogue

1 个答案:

答案 0 :(得分:2)

你错过了构造函数

export class AppComponent {
    data: Promise<string>;

    constructor() { // << You missed it!
        this.data = new Promise((resolve,reject)=>{
        setTimeout(() => resolve("Hello World "), 500);
        });
    }
}

或者您也可以直接将Promise分配给属性

    data: Promise<string> = new Promise((resolve,reject)=>{
        setTimeout(() => resolve("Hello World "), 500);
    });

这是您的plnkr工作。