我是angularJS的新手,我开始了解工厂,但我的代码不起作用。
我的js文件:
angular
.module('pruebasAngularApp',[])
.factory('FacDePrueba',function($http){
var FacDePrueba = {};
FacDePrueba.getInfoApi = function(){
return $http.get('http://jsonplaceholder.typicode.com');
};
})
.controller('pruebasAngularCtrl', function($scope,FacDePrueba){
this.DatosApi = FacDePrueba.getInfoApi();
})
Chrome上的显示:
TypeError: Cannot read property 'getInfoApi' of undefined
at new <anonymous> (controller.js:10)
at d (angular.js:3966)
at Object.instantiate (angular.js:3977)
at angular.js:7281
at angular.js:6670
at r (angular.js:332)
at N (angular.js:6657)
at g (angular.js:6105)
at g (angular.js:6108)
at angular.js:6001
你能帮助我吗?
答案 0 :(得分:3)
您忘记从factory
返回某些内容,由于factory
返回undefined
,您将收到错误。
....
.factory('FacDePrueba',function($http){
var FacDePrueba = {};
FacDePrueba.getInfoApi = function(){
return $http.get('http://jsonplaceholder.typicode.com');
};
// add return statement.
return FacDePrueba;
})...