这是模块AngularJs错误无法读取属性 '然后' 未定义
(function(){
/**
* app Module
*
* Description
*/
angular.module('app',[]);
})();
我有这样的服务
(function(){
'use strict';
angular
.module('app')
.factory('homeservice',homeservice);
homeservice.$inject = ['$http']
function homeservice($http){
var service = {
test : test
}
return service;
/**
* get articles
* @return {[type]} [description]
*/
function test(){
$http.get('/test')
.then(testSuccess)
.catch(testError)
function testSuccess(response){
return response.data;
//console.log(response.data) this line prints data to console
}
function testError(error){
return error;
}
}
}
})();
这是我的控制器,它调用函数
(function(){
angular.module('app')
.controller('HomeController',HomeController);
HomeController.$inject = ['homeservice']
function HomeController(homeservice) {
var vm = this;
test();
function test(){
homeservice.test().then(function(response){
console.log(response);
});
}
}
})();
我一直出现错误 - 无法读取未定义的属性'then'当我在我的控制器中调用方法。我以前使用过这种方式,它工作正常。
ANSWER
function test(){
return $http.get('/test') // return was missing here
.then(testSuccess)
.catch(testError)
function testSuccess(response){
return response.data;
//console.log(response.data) this line prints data to console
}
function testError(error){
return error;
}
}
我错过了在$ http.get( '/测试')回报
做你所定义的模块? – Sajeetharan
@Sajeetharan是的模块被定义,忘记添加它。 –