obseravable.subscribe的范围

时间:2016-08-03 12:22:26

标签: angular observable

我正在尝试检索然后从永久存储中更新对象但无法访问.subscribe中的对象。我对此很新,并且不确定语法。任何人都可以建议吗?如何在范围内引入“this.socialMediaAccount”,以便我可以链接另一个HTTP调用来执行实际更新。

export class SocialMediaAccountTwitterAuthorisedComponent implements OnInit {

    public errorMessage: string;
    public socialMediaAccount;

    constructor ( private socialMediaAccountService: SocialMediaAccountService, private router: Router ) {}

    ngOnInit() {

        this.socialMediaAccountService.findBy({'column': 'oauth_token', 'comparison_operator': 'equals', 'value': this.router.routerState.snapshot.queryParams.oauth_token })
            .subscribe(
                socialMediaAccount => this.socialMediaAccount = socialMediaAccount,
                error =>  this.errorMessage = <any>error;

                // I need to update the object "socialMediaAccount"
                console.log( this.socialMediaAccount );
            );
    }
}

非常感谢。

1 个答案:

答案 0 :(得分:0)

您需要将代码添加到传递给subscribe的回调中,该回调在数据到达时调用:

export class SocialMediaAccountTwitterAuthorisedComponent implements OnInit {

    public errorMessage: string;
    public socialMediaAccount;

    constructor ( private socialMediaAccountService: SocialMediaAccountService, private router: Router ) {}

    ngOnInit() {

        this.socialMediaAccountService.findBy({'column': 'oauth_token', 'comparison_operator': 'equals', 'value': this.router.routerState.snapshot.queryParams.oauth_token })
            .subscribe(
                socialMediaAccount => {
                  this.socialMediaAccount = socialMediaAccount;
                  console.log( this.socialMediaAccount );
                  // I need to update the object "socialMediaAccount"
                },
                error =>  this.errorMessage = <any>error
            );
    }
}