无法通过AngularJs中的指令从工厂访问函数
/TypeError:无法读取未定义的属性“getToken”/ 我试图调用在工厂函数中定义的getToken函数无法通过AngularJs中的指令从工厂访问函数,angularjs,Angularjs,/TypeError:无法读取未定义的属性“getToken”/ 我试图调用在工厂函数中定义的getToken函数 angular.module('pesaveWeb') .directive('goals', function goalsDrctv ($timeout) { 'use strict'; return { restrict: 'E', replace: true, scope: true, templateUrl
angular.module('pesaveWeb')
.directive('goals', function goalsDrctv ($timeout) {
'use strict';
return {
restrict: 'E',
replace: true,
scope: true,
templateUrl: "js/directives/goals.tmpl.html",
controllerAs: 'savings',
controller: function ($routeParams, $scope,
savingsFactory,tokenFactory) {
this.message = {};
var token=tokenFactory.getToken();
var getGoals = savingsFactory.getGoals(token);
if (getGoals) {
getGoals.then( angular.bind(this, function (response) {
savingsFactory.message = response;
this.message = savingsFactory.message;
alert(JSON.stringify(this.message));
}) );
}
}
}
});
angular.module('pesaveWeb').factory('tokenFactory', function tokenFactory ($http,$routeParams) {
'use strict';
var obj = {};
obj.getToken = function () {
return $http({
method: 'POST',
url: "../api/v1/getToken",
headers : {
'Content-Type':'application/json',
'X-API-KEY':'04g4g00c04ks4sokgkoosg0kwww0cww4www0kc80',
'Authorization':"Basic cGVzYXZlQXBwOkNDNTVzV0FwUW0zYWxpazlLNTcwTTFXQ1RNOUJ1TmZS"
},
data: {"grant_type":"client_credentials"}
}) .success(function (data) {
})
.error(function (data) {
});
};
});
指令和工厂都在单独的.js文件中
但它抛出了一个错误,无法访问该函数
angular.module('pesaveWeb')
.directive('goals', function goalsDrctv ($timeout) {
'use strict';
return {
restrict: 'E',
replace: true,
scope: true,
templateUrl: "js/directives/goals.tmpl.html",
controllerAs: 'savings',
controller: function ($routeParams, $scope,
savingsFactory,tokenFactory) {
this.message = {};
var token=tokenFactory.getToken();
var getGoals = savingsFactory.getGoals(token);
if (getGoals) {
getGoals.then( angular.bind(this, function (response) {
savingsFactory.message = response;
this.message = savingsFactory.message;
alert(JSON.stringify(this.message));
}) );
}
}
}
});
angular.module('pesaveWeb').factory('tokenFactory', function tokenFactory ($http,$routeParams) {
'use strict';
var obj = {};
obj.getToken = function () {
return $http({
method: 'POST',
url: "../api/v1/getToken",
headers : {
'Content-Type':'application/json',
'X-API-KEY':'04g4g00c04ks4sokgkoosg0kwww0cww4www0kc80',
'Authorization':"Basic cGVzYXZlQXBwOkNDNTVzV0FwUW0zYWxpazlLNTcwTTFXQ1RNOUJ1TmZS"
},
data: {"grant_type":"client_credentials"}
}) .success(function (data) {
})
.error(function (data) {
});
};
});
工厂函数需要返回用于将函数绑定到的
obj
angular.module('pesaveWeb').factory('tokenFactory', function tokenFactory ($http,$routeParams) {
'use strict';
var obj = {};
obj.getToken = function () {
return $http({
method: 'POST',
url: "../api/v1/getToken",
headers : {
'Content-Type':'application/json',
'X-API-KEY':'04g4g00c04ks4sokgkoosg0kwww0cww4www0kc80',
'Authorization':"Basic cGVzYXZlQXBwOkNDNTVzV0FwUW0zYWxpazlLNTcwTTFXQ1RNOUJ1TmZS"
},
data: {"grant_type":"client_credentials"}
}) .success(function (data) {
})
.error(function (data) {
});
};
return obj;
});
另外,另一个潜在问题是
getToken()
函数可能无法按预期工作。您应该使用Angular的$q
服务在getToken()
函数中解析令牌值时使用“承诺”。查看其您的工厂函数需要返回用于将函数绑定到的obj
angular.module('pesaveWeb').factory('tokenFactory', function tokenFactory ($http,$routeParams) {
'use strict';
var obj = {};
obj.getToken = function () {
return $http({
method: 'POST',
url: "../api/v1/getToken",
headers : {
'Content-Type':'application/json',
'X-API-KEY':'04g4g00c04ks4sokgkoosg0kwww0cww4www0kc80',
'Authorization':"Basic cGVzYXZlQXBwOkNDNTVzV0FwUW0zYWxpazlLNTcwTTFXQ1RNOUJ1TmZS"
},
data: {"grant_type":"client_credentials"}
}) .success(function (data) {
})
.error(function (data) {
});
};
return obj;
});
另外,另一个潜在问题是
getToken()
函数可能无法按预期工作。您应该使用Angular的$q
服务在getToken()
函数中解析令牌值时使用“承诺”。查看其您需要从工厂返回obj
。添加一个返回对象代码>声明到工厂代码的末尾。已完成..谢谢!!!代码中的另一个潜在问题。查看我发布的答案。嘿,你能告诉我原因吗..我被困在其他地方,原因是你返回的是$http()
,而不是回调中收到的数据。因此,您需要在success()
回调中使用承诺返回data
。。。如果您面临的问题与原帖子中提到的问题不同,请接受答案并在另一个问题中发布该特定问题。您需要从工厂返回obj
。添加一个返回对象代码>声明到工厂代码的末尾。已完成..谢谢!!!代码中的另一个潜在问题。查看我发布的答案。嘿,你能告诉我原因吗..我被困在其他地方,原因是你返回的是$http()
,而不是回调中收到的数据。因此,您需要在success()
回调中使用承诺返回data
。。。如果你面临的问题与原帖子中提到的不同,请接受答案并在另一个问题中发布具体问题。