Angular2 异步验证器未挂起

Angular2 Async Validator not Pending

我正在学习 Angular2 的在线课程,在我一起编写代码的课程中,我遇到了一个错误,但无法弄清楚哪里出错了。

我试图在网上查看类似的问题,但找不到任何有效的解决方案。我最接近的是这个答案,,但这些建议要么无关紧要,要么不起作用。

这是我的代码:

组件

export class SignUpFormComponent {
   form: ControlGroup;

constructor(fb: FormBuilder){
    this.form = fb.group({
        username: ['', Validators.compose([
            Validators.required, 
            UsernameValidators.cannotContainSpace
        ]), UsernameValidators.shouldBeUnique],
        password: ['', Validators.required]
    });
}

signup(){
    console.log(this.form.value);
}
}

验证者

export class UsernameValidators{
static shouldBeUnique(control: Control){
    console.log("taken");
    return new Promise((resolve, reject) => {
        setTimeout(function(){
            if(control.value == "example")
                resolve({ shouldBeUnique: true });
            else
                resolve(null);
        }, 1000);
    });
}
static cannotContainSpace(control: Control){
    if(control.value.indexOf(" ") >= 0){
        return { cannotContainSpace: true };
    }
    return null;
}
}

表格的相关部分

    <input 
        ngControl="username"
        id="username" 
        type="text" 
        class="form-control"
        #username="ngForm">

    <div *ngIf="username.control.pending">Checking for uniqueness...</div>

   <div 
        *ngIf="username.errors.shouldBeUnique"
        class="alert alert-danger">
        Username is already taken.
    </div>

当我 运行 本地服务器并开始在表单中输入 div 和未决消息时,如果我在输入字段中输入 "example" 错误消息不会弹出。我错过了什么?谢谢

因为我必须将 tsconfig.json 文件从 "target" : "es5" 更改为 "target" : "es6",我 运行 npm install 再次在终端中,然后 运行 npm start,这解决了问题。