0

Angularjs を使用して、Heroku に配置した Rails テスト アプリから JSON を取得しようとしています。以下に、Angular と Rails のコードを示します。

これは、Firebug コンソールに表示されるエラーです。「NetworkError: 404 が見つかりません - http://desolate-earth-2852.herokuapp.com/declarations.json

これが機能しない理由はありますか?

Angularjs コード

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

App.controller('TodoCtrl', function($scope, $http) {
  $http.get('http://desolate-earth-2852.herokuapp.com/declarations.json')
       .then(function(res){
          $scope.todos = res.data;        
        });
});

Railsコード

  def index
    @declarations = Declaration.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @declarations }
    end
  end

ここにルートがあります

Test::Application.routes.draw do
  root :to => 'welcome#index'
  get "welcome/index"
  resources :declarations
end
4

1 に答える 1

5

Rails JS の縮小が原因かもしれません。Ryan Bates がRailscast で AngularJS の統合について語っています。構成ファイルで縮小化を考慮していることに言及していないことに気付きました。そうでない場合は、Angular コードのすべての依存性注入を次のように配列に配置する必要があることを意味します。

App.controller('TodoCtrl', ['$scope', '$http', function($scope, $http) {
  $http.get('http://desolate-earth-2852.herokuapp.com/declarations.json')
    .then(function(res){
       $scope.todos = res.data;        
    });
}]);

これの代わりに:

App.controller('TodoCtrl', function($scope, $http) {
  $http.get('http://desolate-earth-2852.herokuapp.com/declarations.json')
   .then(function(res){
      $scope.todos = res.data;        
   });
});

これにより、AngularJS に依存関係が何であるかが伝えられるため、(縮小中に) 名前が変更されても問題はありません。アプリのコントローラーを作成するためにここにあるものなど、サービスを受け入れるすべての関数に対してこれを行う必要があります。

于 2013-10-29T09:14:52.337 に答える