AngularJS上的工厂无法正常工作。错误“无法读取未定义的属性'getInfoApi'”

时间:2015-12-19 16:25:03

标签: javascript angularjs factory

我是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

你能帮助我吗?

1 个答案:

答案 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;
})...