Javascript AngularJS上的工厂不工作。“错误”;无法读取属性';getInfoApi';“未定义”的定义;
我是angularJS的新手,我开始学习工厂,但我的代码不起作用 我的js文件:Javascript AngularJS上的工厂不工作。“错误”;无法读取属性';getInfoApi';“未定义”的定义;,javascript,angularjs,factory,Javascript,Angularjs,Factory,我是angularJS的新手,我开始学习工厂,但我的代码不起作用 我的js文件: angular .module('pruebasAngularApp',[]) .factory('FacDePrueba',function($http){ var FacDePrueba = {}; FacDePrueba.getInfoApi = function(){ return $http.get('http://jsonplaceholder.typicode.com'
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
TypeError:无法读取未定义的属性“getInfoApi”
新建时(controller.js:10)
在d处(角度:3966)
在Object.instantiate(angular.js:3977)
在angular.js:7281
在angular.js:6670
在r(angular.js:332)
在N(角度:6657)
在g处(角度:6105)
在g处(angular.js:6108)
角度:js:6001
你能帮我吗?你忘了从
工厂
返回一些东西,你会因为工厂
返回未定义的而得到错误
....
.factory('FacDePrueba',function($http){
var FacDePrueba = {};
FacDePrueba.getInfoApi = function(){
return $http.get('http://jsonplaceholder.typicode.com');
};
// add return statement.
return FacDePrueba;
})...
您忘记从工厂
返回某些内容,您将得到错误,因为工厂
返回未定义
....
.factory('FacDePrueba',function($http){
var FacDePrueba = {};
FacDePrueba.getInfoApi = function(){
return $http.get('http://jsonplaceholder.typicode.com');
};
// add return statement.
return FacDePrueba;
})...