等待Api - Angular js 2的响应

时间:2016-08-24 16:13:39

标签: angular typescript cookies

我想在cookie中添加一个令牌唯一的问题,并且在获得API的响应之前创建了cookie,我可以做些什么来等待响应然后创建cookie?

我的代码

getlogin()
{
this._symfonyservice.login(this.user,this.pass).subscribe(//call the post
            data => this.results = data, // put the data returned from the server in our variable
            error => console.log("Error HTTP Post Service"), // in case of failure show this message
            () => console.log(this.results['token'])//run this code in all cases
        );
if(this.results['token']!="")
{
    let token = this.results['token'];
    this.deleteCookie("Cookieteste");
    this.setCookie("Cookieteste",token, 1);
}
   }

1 个答案:

答案 0 :(得分:1)

data而不是() final function

中执行此操作
getlogin()
{
this._symfonyservice.login(this.user,this.pass).subscribe(//call the post
data => {
            this.results = data
            if(this.results['token']!="")
            {
                let token = this.results['token'];
                this.deleteCookie("Cookieteste");
                this.setCookie("Cookieteste",token, 1);
            }       
        }, // put the data returned from the server in our variable
                error => console.log("Error HTTP Post Service"), // in case of failure show this message
                () => console.log('completed')//run this code in all cases
     );