调用数据服务后变量的值变得不确定 [英] Value of variable becomes undefined after calling data service

查看:70
本文介绍了调用数据服务后变量的值变得不确定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个角度js控制器"TakeSurveyController",它调用数据服务"takeSurveyDataService".控制器具有变量empId,我正在使用'this'关键字成功访问该变量.但是,在我调用数据服务后不久,此变量的值就变得不确定.

I have a angular js controller 'TakeSurveyController' which calls a data service 'takeSurveyDataService'. The controller has a variable empId which I am accessing successfully using the 'this' keyword. However, the value of this variable becomes undefined soon after I make a call to the data service.

(function(){
dataUrls = {

    employeeUrl : '/SurveyPortal/Company/Employees/'    
}

angular.module('takeSurvey',[])
.service('takeSurveyDataService',function($http,$log){
    this.checkEmployee = function(employeeId){
        var url = dataUrls.employeeUrl + employeeId;
        return $http.get(url);
    };

})
.controller('TakeSurveyController',function($http,takeSurveyDataService,$location){
    this.empId = '';
    this.checkEmployee = function(){
        takeSurveyDataService.checkEmployee(this.empId).then(this.attemptSurvey);//empId has value here. Call successful to data service
    };
    this.attemptSurvey = function(employeeResponse) {
        if(employeeResponse.data=="true"){
            $location.path('/attemptSurvey/'+this.empId); //empId variable undefined here, unable to access value that was available above

        }
        else{
            this.empId = '';
            alert("This empId has not been registered. Please register before taking survey!");
        }

    };
})
})();

以下是html代码.

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<div align="center" style="margin-top:50px">
    <form name="checkEmployee" ng-controller="TakeSurveyController as takeSurveyController" ng-submit="takeSurveyController.checkEmployee()" >
        <input type="text" ng-model="takeSurveyController.empId" placeholder="Employee ID" /><br>
        <input type="submit" class="btn btn-large btn-success" value="Move to survey" />
    </form>
</div>
</body>
</html>

推荐答案

您必须非常小心地使用此内部函数,因为"this"一词每次都取决于上下文而具有不同的含义.我相信在答应回调中,"this"的含义不同,它与TakeSurveyController的连接松动.请尝试将'this'分配给'self'变量,这在angular和js中总是很好的做法:

You must be very carefull using this inside functions since 'this' words get different meaning each time depending on context. I believe during promise callback 'this' get's different meaning and it is loosing connection with TakeSurveyController. Please try to assign 'this' to 'self' variable which is always good practice in angular and js:

(function(){
dataUrls = {

    employeeUrl : '/SurveyPortal/Company/Employees/'    
}

angular.module('takeSurvey',[])
.service('takeSurveyDataService',function($http,$log){
    var self = this;
    self.checkEmployee = function(employeeId){
        var url = dataUrls.employeeUrl + employeeId;
        return $http.get(url);
    };

})
.controller('TakeSurveyController',function($http,takeSurveyDataService,$location){
    var self = this;
    self.empId = '';
    self.checkEmployee = function(){
        takeSurveyDataService.checkEmployee(self.empId).then(self.attemptSurvey);//empId has value here. Call successful to data service
    };
    self.attemptSurvey = function(employeeResponse) {
        if(employeeResponse.data=="true"){
            $location.path('/attemptSurvey/'+self.empId); //empId variable undefined here, unable to access value that was available above

        }
        else{
            self.empId = '';
            alert("This empId has not been registered. Please register before taking survey!");
        }

    };
})
})();

这篇关于调用数据服务后变量的值变得不确定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆