获取Window函数以在本地分配变量

时间:2016-09-09 02:53:54

标签: javascript angular typescript scope

我正在尝试让我的Angular2服务使用由linkedin提供的脚本加载的LinkedIN javascript SDK。该功能适用​​于获取数据并返回它但我无法将本地函数变量设置为我从linkeidn获取的数据

export class LinkedInAuthService {

constructor(

) {

}

linkedInAuthorization() {
    window['IN'].User.authorize();
    window['IN'].Event.on(window['IN'], 'auth', this.getLinkedInUserInfo);
}
getLinkedInUserInfo() {
    let linkedinInfo:any;
    window['IN']
        .API
        .Raw()
        .url("/people/~:(firstName,lastName,emailAddress,industry,specialties,positions)?format=json")
        .result(function(data:any){
            console.log(data) //logs data correctly
            linkedinInfo = data 
        });
    console.log(linkedinInfo) // undefined
}

AuthLinkedInUserforGlx() {
    console.log(localStorage.getItem('linkedinInfo'))
}
}

我认为问题是当我设置linkedinInfo = data时它没有在本地设置它,只是不确定如何使它本地化

1 个答案:

答案 0 :(得分:0)

这对我有用

export class LinkedInService {
IN = window['IN'];
userProfile: Object;
constructor() {
    this.userProfile = JSON.parse(localStorage.getItem('profile'))
    this.IN.Event.on(this.IN, 'auth', this.getLinkedInUserInfo);
    console.log('end of constructor')
}

linkedInAuthorization(){
    this.IN.User.authorize();
    console.log('end of linkedInAuthorization service')
}

getLinkedInUserInfo() {
    this.IN.API.Raw()
        .url("/people/~:(firstName,lastName,emailAddress,industry,specialties,positions)?format=json")
        .result((data:any) => {
            localStorage.setItem('profile', JSON.stringify(data));
            this.userProfile = data;
        });
    console.log('end of getLinkedInUserInfo service');
    this.AuthLinkedInOnGlx
}

AuthLinkedInOnGlx (){

}



}