subsrcibe中变量的Angular2值未定义

时间:2017-10-05 07:48:17

标签: angular undefined subscribe

早上好,

我有以下问题:

@interface UIViewControllerName ()

// HERE YOU CAN DRAG YOUR IBOutlets

@end 

// Here begins the viewcontrollers implementation
@implementation UIViewControllerName

- (void) viewDidLoad {
..... etc. etc..

onSubmit(){ this.userService.getCandidate(this.candidate) .subscribe( (candidate) => { this.candidate2 = candidate; console.log(this.candidate2); this.name=this.candidate.name; console.log(this.candidate2.name); }, (error: Response) => console.log(error), ()=> { console.log("Finished")} ) } 为我提供了所有字段的预期候选人 但是console.log(this.candidate2);给了我未定义的。

我该怎么办,因为我是候选人不同领域的价值。

1 个答案:

答案 0 :(得分:0)

实际上错误来自这一行

 this.name=this.candidate.name;

将其更改为

this.name=this.candidate2.name;

this.name=candidate.name;

应该是,

onSubmit(){ 
  this.userService.getCandidate(this.candidate)
    .subscribe( 
      (candidate) => { 
        this.candidate2 = candidate;
        console.log(this.candidate2);
        this.name=this.candidate2.name;
        console.log(this.candidate2.name);
      },
      (error: Response) =>  console.log(error),
      ()=> {
            console.log("Finished")}
      )
}