【问题标题】:TypeError: Cannot read property 'get' of undefined AngularjsTypeError:无法读取未定义 Angularjs 的属性“get”
【发布时间】:2016-01-05 04:35:52
【问题描述】:

我尝试同时使用 Oboe.Js 和 Angular.Js

这是我正常工作的示例代码..

angular.module('myApp', ['ng-oboe'])
 .config(function (oboeProvider) {
             /* If we were so inclined, we could change the oboe defaults here - headers, etc. */
             // oboeProvider.defaults = {};
     })
 .controller('myCtrl', function ($scope, oboe) {
                $scope.test = "Yo.";
                $scope.items = [];

                oboe.get('http://localhost:4243/containers/f78257b77b21/stats')
                        .start(function (data, etc) {
                        })
                        .node('!', function (value) {   
                        })
                        .done(function (value) {
                                console.log("It works! ", value.read);
                                $scope.items.push(value);
                        })
                        .fail(function (error) {
                                console.log("Error: ", error);
                        });
        });

但是当我尝试将这些代码与我的实际项目控制器一起使用时。我收到这个错误。我不知道为什么我会得到这个。

错误:TypeError:无法读取未定义的属性“get”

模块.js

angular.module('RDash', ['ui.bootstrap', 'ui.router', 'ngCookies','ng-oboe'])
  .config(function (oboeProvider) {
      /* If we were so inclined, we could change the oboe defaults here - headers, etc. */
      // oboeProvider.defaults = {};
  });

stats-ctrl.js

  angular
  .module('RDash')
  .controller('StatsCtrl', ['$scope', '$stateParams', StatsCtrl]);

  function StatsCtrl($scope, $stateParams, oboe) {

    var id = $stateParams.id;

    $scope.myData = [];

    $scope.items = [];

    console.log('http://localhost:4243/containers/' + id + '/stats');

  oboe.get('http://localhost:4243/containers/' + id + '/stats')
        .start(function (data, etc) {
            console.log("Dude! We're goin'!", data, etc);
        })
        .node('!', function (value) {

        })
        .done(function (value) {
            console.log("It works! ", value.read);
        })
        .fail(function (error) {
            console.log("Error: ", error);
        });
}

【问题讨论】:

  • 您似乎没有在控制器设置中注入双簧管。
  • @MatthewGreen 抱歉,我忘记添加 module.js 文件。我已经注入了“ng-oboe”我的 module.js 文件。我需要再次注入控制器文件吗?
  • 我说的不是你的模块,我说的是控制器。你有$scope, $stateParams,但没有oboe。
  • 哦,好吧!谢谢!它逃脱了我! @MatthewGreen

标签: javascript angularjs dependency-injection controller angularjs-controller


【解决方案1】:

在你的控制器函数中,obe factory 没有值(未定义),因为它没有被注入到 DI 数组中。

angular
  .module('RDash')
  .controller('StatsCtrl', ['$scope', '$stateParams', 'obe', StatsCtrl]); //<-inject obe here
      function StatsCtrl($scope, $stateParams, oboe) {

【讨论】:

  • 是的,我很抱歉。这是我的错:(
  • @ErolGuzoğlu 很高兴为您提供帮助..谢谢 :)
猜你喜欢
  • 1970-01-01
  • 2017-04-28
  • 1970-01-01
  • 2020-07-14
  • 2017-08-30
  • 2015-08-26
  • 2016-09-12
  • 2016-11-20
  • 1970-01-01
相关资源
最近更新 更多