Angular 访问ngrx可观察变量时总是返回未定义

Angular 访问ngrx可观察变量时总是返回未定义,angular,angular2-forms,typescript1.8,ngrx,Angular,Angular2 Forms,Typescript1.8,Ngrx,我有以下资料: language.component.ts 自定义验证器.ts 尽管错误是由onPropertyChanged()生成的,但在language.component.ts中指定值的msg变量始终未定义 我怎样才能纠正这个问题?感谢您的帮助 谢谢我认为您的问题在于如何在此处设置消息: errors$ .filter(errors => errors.length > 0) .subscribe( errors => {

我有以下资料:

language.component.ts 自定义验证器.ts 尽管错误是由onPropertyChanged()生成的,但在language.component.ts中指定值的msg变量始终未定义

我怎样才能纠正这个问题?感谢您的帮助


谢谢

我认为您的问题在于如何在此处设置消息:

errors$
      .filter(errors => errors.length > 0)
      .subscribe(
          errors => {
            errors.map(
                error => {
                  msg = doGetErrorMsg(error)
                })
          })

  msg = doGetErrorMsg(ctrlErrors);

  return msg
我认为,在订阅完成消息设置之前执行“msg=doGetErrorMsg(ctrlErrors);”时,将始终返回null

您应该为控件使用异步验证器,并让它返回一个承诺。大概是这样的:

this.first =
        new FormControl('',
Validators.compose([Validators.required, Validators.minLength(2), CustomValidators.nounValidator]),
Validators.composeAsync([(control: Control) => this.IsValid(control.value)])
        );

public IsValid(id: string): Promise<any> {
    if (id.length > 2) {
        return new Promise(resolve => {
            this.GetPartById(id)
                .subscribe((data: Part) => {
                    if (!data) {
                        resolve({dataExists: false});
                    } else {
                        // need to return null if ok
                        resolve(null);
                    }
                }, (error: any) => {
                });
        });
    }
}
this.first=
新表单控件(“”,
组合([Validators.required,Validators.minLength(2),CustomValidators.nounValidator]),
composeSync([(control:control)=>this.IsValid(control.value)])
);
public-IsValid(id:string):承诺{
如果(id.length>2){
返回新承诺(解决=>{
this.GetPartById(id)
.订阅((数据:部分)=>{
如果(!数据){
解析({dataExists:false});
}否则{
//如果确定,则需要返回null
解析(空);
}
},(错误:任意)=>{
});
});
}
}

我认为您的问题在于如何在此处设置消息:

errors$
      .filter(errors => errors.length > 0)
      .subscribe(
          errors => {
            errors.map(
                error => {
                  msg = doGetErrorMsg(error)
                })
          })

  msg = doGetErrorMsg(ctrlErrors);

  return msg
我认为,在订阅完成消息设置之前执行“msg=doGetErrorMsg(ctrlErrors);”时,将始终返回null

您应该为控件使用异步验证器,并让它返回一个承诺。大概是这样的:

this.first =
        new FormControl('',
Validators.compose([Validators.required, Validators.minLength(2), CustomValidators.nounValidator]),
Validators.composeAsync([(control: Control) => this.IsValid(control.value)])
        );

public IsValid(id: string): Promise<any> {
    if (id.length > 2) {
        return new Promise(resolve => {
            this.GetPartById(id)
                .subscribe((data: Part) => {
                    if (!data) {
                        resolve({dataExists: false});
                    } else {
                        // need to return null if ok
                        resolve(null);
                    }
                }, (error: any) => {
                });
        });
    }
}
this.first=
新表单控件(“”,
组合([Validators.required,Validators.minLength(2),CustomValidators.nounValidator]),
composeSync([(control:control)=>this.IsValid(control.value)])
);
public-IsValid(id:string):承诺{
如果(id.length>2){
返回新承诺(解决=>{
this.GetPartById(id)
.订阅((数据:部分)=>{
如果(!数据){
解析({dataExists:false});
}否则{
//如果确定,则需要返回null
解析(空);
}
},(错误:任意)=>{
});
});
}
}