将Rails/AngularJS应用程序部署到Heroku时出现未知的提供程序错误

Joh*_*ohn 23 ruby-on-rails-3 angularjs

我有一个Rails/AngularJS应用程序,在本地开发环境中工作正常.但是,当我将此应用程序部署到Heroku时,AngularJS无法正常工作,则返回此错误:

Unknown provider: eProvider <- e
Run Code Online (Sandbox Code Playgroud)

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

这是控制器的外观:

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

这是视图中的代码:

    %section.reminders
      %div{"ng-controller" => "RemindersCtrl"}
        %ul
          %li{"ng-repeat" => "reminder in reminders"}
            .title {{reminder.title}}
Run Code Online (Sandbox Code Playgroud)

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

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

Joh*_*ohn 27

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

function RemindersCtrl($scope, $http) {
  ...
}
RemindersCtrl.$inject = ['$scope', '$http'];
Run Code Online (Sandbox Code Playgroud)

或者不是像这样定义一个函数:

function RemindersCtrl($scope, $http) {
  ...
}
Run Code Online (Sandbox Code Playgroud)

它应该这样做:

var RemindersCtrl = ['$scope', '$http', function($scope, $http) {
  ...
}];
Run Code Online (Sandbox Code Playgroud)

  • 我怎么知道哪个文件引发了这个错误?它显示为在application.js上 (2认同)

Ren*_*des 5

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

看到这里的 mroe


tar*_*aro 5

Angular团队(一般而言)建议我们不要污染全球范围.

.controller方法,

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

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

对我来说很好.这在Angular Understanding Controllers文档中有记录