【问题标题】:Unknown provider error when deploying Rails/AngularJS app to Heroku将 Rails/AngularJS 应用程序部署到 Heroku 时出现未知的提供程序错误
【发布时间】:2012-11-16 11:30:07
【问题描述】:

我有一个在本地开发环境中运行良好的 Rails/AngularJS 应用程序。 但是,当我将此应用程序部署到 Heroku 时,AngularJS 不起作用并返回此错误:

Unknown provider: eProvider <- e

我做了一些研究,似乎它与资产的预编译和缩小有关,但我不知道该怎么做才能解决这个问题。有任何想法吗?谢谢!

这是控制器的外观:

function RemindersCtrl($scope, $http) {
  $http.get('/reminders.json').success(function(data) {
    $scope.reminders = data;
    console.log(data);
  });
}

这是视图中的代码:

    %section.reminders
      %div{"ng-controller" => "RemindersCtrl"}
        %ul
          %li{"ng-repeat" => "reminder in reminders"}
            .title {{reminder.title}}

更新:我将控制器更改为此,但结果相同:

var RemindersCtrl = function($scope, $http) {
  $http.get('/reminders.json').success(function(data) {
    $scope.reminders = data;
    console.log(data);
  });
}
RemindersCtrl.$inject = ['$scope','$http'];

【问题讨论】:

  • 您的应用是否在任何地方可见,以便我们查看?
  • 目前,我通过不压缩资产“解决”了它。这对于简单的试用应用程序来说没有问题,但对于真正的应用程序来说不是一个选项。

标签: ruby-on-rails-3 angularjs


【解决方案1】:

您可能将控制器定义为FooController = function($http) {},您应该定义为FooController = ["$http", function($http){}]

见mroe here

【讨论】:

  • 由于错误与提供者有关,它应该在module.config。 PS:数组声明风格最适合这种情况;)
【解决方案2】:

根据 AngularJS 教程 (http://docs.angularjs.org/tutorial/step_05),您可以将其添加到控制器以防止出现缩小问题:

function RemindersCtrl($scope, $http) {
  ...
}
RemindersCtrl.$inject = ['$scope', '$http'];

或者不要像这样定义函数:

function RemindersCtrl($scope, $http) {
  ...
}

应该这样做:

var RemindersCtrl = ['$scope', '$http', function($scope, $http) {
  ...
}];

【讨论】:

  • 确保你也将此模式应用到指令特定的控制器!
  • 我怎么知道哪个文件抛出了这个错误?它就像在 application.js 上一样显示
【解决方案3】:

Angular 团队(一般来说也是如此)建议我们不要污染全局范围。

.控制器方法,

var myApp = angular.module('myApp',[]);

myApp.controller('GreetingCtrl', ['$scope', function($scope) {
  $scope.greeting = 'Hola!';
}]);

对我来说效果很好。这记录在Angular Understanding Controllers documentation

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-22
    • 2015-12-09
    • 2022-09-25
    • 2013-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多