类型错误:无法读取未定义属性'get' Angularjs

3

我正在尝试同时使用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”

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

你的控制器设置中似乎没有注入oboe。 - Matthew Green
@MatthewGreen 抱歉,我忘记添加 module.js 文件了。我已经在我的 module.js 文件中注入了 'ng-oboe' 。我需要在控制器文件中再注入一次吗? - Erol Guzoğlu
我不是在谈论你的模块,我在谈论控制器。你有$scope, $stateParams但没有oboe - Matthew Green
哦,好的!谢谢!我忘了这件事了!@MatthewGreen - Erol Guzoğlu
1个回答

2

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

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

是的,我非常抱歉。这是我的错 :( - Erol Guzoğlu
@ErolGuzoğlu 很高兴能帮助你。谢谢 :) - Pankaj Parkar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接