Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.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/7/neo4j/3.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 Can';无法从$http回调访问此文件_Angularjs_Typescript_Promise - Fatal编程技术网

Angularjs Can';无法从$http回调访问此文件

Angularjs Can';无法从$http回调访问此文件,angularjs,typescript,promise,Angularjs,Typescript,Promise,我将angular 1.5与typescript一起使用,我无法从$http promise返回的回调中访问这个属性 当我试图从回调访问私有方法时,“this”是未定义的 我有以下ServerAPI服务: export class ServerAPI implements IServerAPI { static $inject:Array<string> = ['$http', '$q']; constructor(private $http:ng.IHttpSer

我将angular 1.5与typescript一起使用,我无法从$http promise返回的回调中访问
这个
属性

当我试图从回调访问私有方法时,“this”是未定义的

我有以下ServerAPI服务:

export class ServerAPI implements IServerAPI {
    static $inject:Array<string> = ['$http', '$q'];

    constructor(private $http:ng.IHttpService,
                private $q:ng.IQService) {
    }

    postHandler(partialUrl:string, data?:any, config?:any):ng.IPromise<any> {
        let url = this.buildUrl(partialUrl);

        var result:ng.IPromise< any > = this.$http.post(url, data, config)
            .then((response:any):ng.IPromise<any> => this.handlerResponded(response, data))
            .catch((error:any):ng.IPromise<any> => this.handlerError(error, data));

        return result;
    }

    private handlerResponded(response:any, params:any):any {
        response.data.requestParams = params;
        return response.data;
    }

    private handlerError(error:any, params:any):any {
        error.requestParams = params;
        return error;
    }
}
导出类ServerAPI实现IServerAPI{
静态$inject:Array=['$http','$q'];
构造函数(私有$http:ng.IHttpService,
私人$q:ng.IQService){
}
postHandler(partialUrl:string,data?:any,config?:any):ng.IPromise{
让url=this.buildUrl(partialUrl);
var result:ng.IPromise=此$http.post(url、数据、配置)
.then((响应:any):ng.IPromise=>this.handler响应(响应,数据))
.catch((错误:any):ng.IPromise=>this.handleerror(错误,数据));
返回结果;
}
private HandlerResponsed(响应:任意,参数:任意):任意{
response.data.requestParams=参数;
返回响应数据;
}
私有handlerError(错误:any,参数:any):any{
error.requestParams=params;
返回误差;
}
}
已被用户使用。服务:

export class UserService implements IUserService {
    static $inject:Array<string> = ['$q', 'serverAPI'];

    constructor(private $q:ng.IQService,
                private serverAPI:blocks.serverAPI.ServerAPI) {
        var vm = this;
        $rootScope.globals = $rootScope.globals || {};
        $rootScope.globals.currentUser = JSON.parse($window.localStorage.getItem('currentUser')) || null;

        this.getUserPermissions();
    }

    private getUserPermissions:() => IPromise<any> = () => {
        var promise = this.serverAPI.postHandler('MetaDataService/GetUserPermissions',
            {userID: this.getUser().profile.UserID})
            .then((res) => {
                this.updateUser('permissions', res.GetUserPermissionsResult); // NOT WORKING, this is undefined
            })
            .catch((response:any):ng.IPromise<any> => {
                this.updateUser('permissions', res.GetUserPermissionsResult); // NOT WORKING, this is undefined
            });
        return promise;
    };

    private updateUser:(property:string, value:any) => void = (property, value) => {
    };
}
导出类UserService实现IUserService{
静态$inject:Array=['$q','serverAPI'];
建造商(私人$q:ng.IQService,
专用服务器API:blocks.serverAPI.serverAPI){
var vm=这个;
$rootScope.globals=$rootScope.globals | |{};
$rootScope.globals.currentUser=JSON.parse($window.localStorage.getItem('currentUser'))| | null;
这是.getUserPermissions();
}
私有getUserPermissions:()=>IPromise=()=>{
var promise=this.serverAPI.postHandler('MetaDataService/GetUserPermissions',
{userID:this.getUser().profile.userID})
。然后((res)=>{
this.updateUser('permissions',res.GetUserPermissionsResult);//不工作,未定义
})
.catch((响应:任意):ng.IPromise=>{
this.updateUser('permissions',res.GetUserPermissionsResult);//不工作,未定义
});
回报承诺;
};
私有updateUser:(属性:字符串,值:任意)=>void=(属性,值)=>{
};
}

问题在于这一行:

.then((response:any):ng.IPromise<any> => this.handlerResponded(response, data))

问题在于这一行:

.then((response:any):ng.IPromise<any> => this.handlerResponded(response, data))

typescript匿名函数不是在匿名函数中包含这个吗?我再也不确定了。我认为更可靠的方法可能是将其保存为本地变量,但通常情况下typescript应该为您的typescript匿名函数执行此转换,而不是在匿名函数中使用_this?我再也不确定了。我认为更可靠的方法可能是将其保存为本地变量,但通常typescript应该为您执行此转换