订阅内的服务调用订阅表单值更改不起作用

时间:2017-09-08 07:14:07

标签: javascript angular typescript angular2-forms

我是Angular2的新手。

基本上我正在尝试订阅iam订阅服务调用内部的表单值更改。我的代码如下

export class abc ... {
...
...
appData:app_Data;
updateform ;

ngOnInit () {

this.updateform = new FormGroup ({

elem1: new FormControl("", Validators.required),
elem2: new FormControl("", Validators.required),
elem3: new FormControl("", Validators.required)
})

let current_this = this;

this.updateform.valueChanges.subscribe(data => {
    console.log('Form changes', data)

    // need to call the rest backend service as soon as 
    // elem1,elem2,elem3 
    // is entered by the user .

    if (current_this.updateform.controls['elem1'].value 
       && current_this.updateform.controls['elem2'].value
       && current_this.updateform.controls['elem3'].value
     ) {
          console.log("calling rest service")
          current_this._appinfoservice.
       getAppDetails(current_this.updateform.controls['elem1'].value
          , current_this.updateform.controls['elem2'].value
          ,current_this.updateform.controls['elem3'].value)
          .subscribe((dataRec)=>current_this.appData=dataRec);

          // if details is already existing populate
          // rest of the fields 
          // and display update / delete buttons 

           if (current_this.appData.det1.length>0 
            && current_this.appData.det2.length>0 
            && current_this.appData.det3.length>0) {

              console.log("exists in  the db")

          }
  }
  })


}

服务代码

 getAppDetails(appName:string,type:string,group:string):Observable<app_Data>{
    let url="http://localhost:9001/xyz/webapi/def/"+appName+"/"+type+"/"+group
    console.log(url)
    return this.http.get(url)
    .map((response:Response) => <app_Data> response.json());
}

问题 - 在这一行subscribe((dataRec)=>current_this.appData=dataRec); 如果我尝试评估current_this它是未定义的,我得到cannot read property appData of undefineddataRec的评估未定义,即使我可以看到其余服务使用postman工具返回数据。

这可能是什么问题?

0 个答案:

没有答案