AngularJS - 工厂 - 类型错误:无法读取的未定义的属性'getSpec“ [英] AngularJS - Factory - TypeError: Cannot read property 'getSpec' of undefined

查看:594
本文介绍了AngularJS - 工厂 - 类型错误:无法读取的未定义的属性'getSpec“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始与AngularJS和我试图从控制器使用工厂时,有一些问题。

I am starting with AngularJS and i am having some issues when trying to use a factory from a controller.

我有以下的工厂

angular.module('testingApp')
  .factory('factoryService', function ($http) {
    // Service logic
    var getSpec = function(p) {
      return $http.get('http://someurl//?p=' + p);
    };
    return {
      getSpec: getSpec  
    };
  });

然后我尝试按以下步骤从控制器消耗它

and then i try to consume it from the controller as follows

angular.module('testingApp')
  .controller('ServiceincientsCtrl',[ function (factoryService,$scope) {
   console.log('Starting Service Incident Controller');
    factoryService.getSpec('AAA').then(function(response){
        $scope.result = response.data;
    }, function(error){
        console.log('opsssss' + error);
    });

  }]);

但是,当我尝试运行它,我收到以下消息

But when i try to run it i receive the following message

类型错误:无法读取的未定义的属性'getSpec

TypeError: Cannot read property 'getSpec' of undefined

我不知道我缺少什么,这应该是一个错误newbbie,我GOOGLE了它,我试过很多例子具有相同的结果。

I don't know what i am missing,It should be a newbbie error, I googled it and i tried many examples with the same result.

什么我做错了任何想法?

Any ideas of what i am doing wrong?

谢谢!

推荐答案

就像你不使用扶养数组符号正常外观。请参考下面的code。请加 factoryService '和; $范围'作为数组项。

Looks like you are not using the dependancy array notation properly. Please refer the below code. Please add 'factoryService' & '$scope' as array items.

.controller('ServiceincientsCtrl', ['factoryService', '$scope', function(factoryService, $scope) {
    console.log('Starting Service Incident Controller');
    factoryService.getSpec('AAA').then(function(response) {
        $scope.result = response.data;
    }, function(error) {
        console.log('opsssss' + error);
    });

}]);

依赖关系注入角文件建立

这篇关于AngularJS - 工厂 - 类型错误:无法读取的未定义的属性'getSpec“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆