這是模塊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是的模塊被定義,忘記添加它。 –