我知道这个重复,但不会与我发布此内容的任何人匹配。我有一个控制器和一个工厂。工厂工作正常,并在控制台得到响应,但当我调用工厂的功能时,已打印错误。请检查以下代码: 控制器:
var app = angular.module('rsplApp',['ngRoute', 'ngCookies', 'ngResource', 'ngSanitize', 'ngValidate']);
app.controller('AddProjectController',['$scope', '$cookies', '$rootScope', '$location','Technologies', function ($scope, $cookies, $rootScope, $location, Technologies){
Technologies.loadTech().then(function(techRes){
$scope.choices = techRes;//console.log(techRes);
})
}])
厂:
app.factory("Technologies",function($http){
var TechCat = {};
TechCat.loadTech = function(){
$http({
method : 'POST',
url : 'api/v1/technologies.php',
data : '', // pass in data as strings
headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
}).success(function(response){
//console.log(response);
return response;
}).error(function(erronrmsg){
return erronrmsg;
})
}
return TechCat;
})
现在当我刷新页面时收到此错误: "错误:Technologies.loadTech(...)未定义 @ http://localhost/vk-angular/scripts/controllers/AddProjectController.js:4:4"
答案 0 :(得分:1)
var app = angular.module('rsplApp',['ngRoute', 'ngCookies', 'ngResource', 'ngSanitize', 'ngValidate']);
app.controller('AddProjectController',['$scope', '$cookies', '$rootScope', '$location','Technologies', function ($scope, $cookies, $rootScope, $location, Technologies){
Technologies.loadTech().then(function(techRes){
$scope.choices = techRes;//console.log(techRes);
})
}]);
app.factory("Technologies",function($http){
var TechCat = {};
TechCat.loadTech = function(){
return $http({
method : 'POST',
url : 'api/v1/technologies.php',
data : '', // pass in data as strings
headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
}).success(function(response){
//console.log(response);
return response;
}).error(function(erronrmsg){
return erronrmsg;
})
};
return TechCat;
});
答案 1 :(得分:0)
您忘记从loadTech()
函数返回$ http承诺。因此,隐式地返回undefined
。但是在您的控制器中,您正在使用loadTech()
,就好像它返回了一个承诺。