Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 第2部分:基于令牌的身份验证_Angularjs - Fatal编程技术网

Angularjs 第2部分:基于令牌的身份验证

Angularjs 第2部分:基于令牌的身份验证,angularjs,Angularjs,我试图在请求的标题中输入身份验证令牌,但由于某些原因,它一直显示为未定义 服务: export class ClientService { token:string constructor (private http: Http, private toastCtrl: ToastController, public storage: Storage ) { this.storage.ready().then(() => { this.stora

我试图在请求的标题中输入身份验证令牌,但由于某些原因,它一直显示为未定义

服务:

export class ClientService {

  token:string

    constructor (private http: Http, private toastCtrl: ToastController, public storage: Storage ) {
        this.storage.ready().then(() => {

      this.storage.get("auth_token").then((val) => {

        // This prints the correct value
        console.log("show token", val) 
        this.token = val
      })
    })

getClients(page):Observable<Response> {
    let headers = new Headers();

    // This prints undefined
    console.log("fetching clients", this.token);

    headers.append("authentication", this.token );
    let options = new RequestOptions({ headers: headers });

    return this.http.get(`${this.baseUrl}?page=${page}`, options)
                    .map(this.extractData)
                    .catch(this.handleError);
}
}
导出类ClientService{
令牌:字符串
构造函数(私有http:http,私有toastCtrl:ToastController,公共存储:存储){
this.storage.ready()。然后(()=>{
这个.storage.get(“auth_令牌”)。然后((val)=>{
//这将打印正确的值
日志(“显示令牌”,val)
this.token=val
})
})
GetClient(第页):可观察{
let headers=新的headers();
//这是未定义的打印
log(“获取客户端”,this.token);
headers.append(“身份验证”,this.token);
let options=newrequestoptions({headers:headers});
返回this.http.get(`${this.baseUrl}?page=${page}`,选项)
.map(此.extractData)
.接住(这个.把手错误);
}
}

getClients在哪里被调用?它可能发生在storage.get承诺解决之前吗?我想这就是问题所在……我找到了一个解决方法,在调用
getClients
时提供
令牌作为参数