2015-10-03 81 views
0

我学习angularjs与轨,但我卡住了,当我要求如何处理angularjs路线与轨道

http://localhost:3000/#/users 

它呈现的默认轨welocome页

下面

是我的app.js文件

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

app.controller('myCtrl', function($scope) { 
    $scope.firstName = "John"; 
    $scope.lastName = "Doe"; 
}); 


app.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) { 
$routeProvider.when('/users',{ 
     templateUrl: 'home.html', 
     controller: 'myCtrl' 
     } 
    ); 
}]); 

application.html.erb

<!DOCTYPE html> 
<html ng-app="myApp"> 
<head> 
    <title>Books</title> 
    <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %> 
    <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %> 
    <%= csrf_meta_tags %> 
</head> 
<body> 

    <div class="container" ng-view> 
    <%= yield %> 
    </div> 

</body> 
</html> 

回答

0

你需要创建一个新的路径:

# config/routes.rb 
root to: 'angular#index' 

,并用一个动作控制器:

# app/controllers/angular_controller.rb 
class AngularController < ApplicationController 
    def index; end 
end 

和模板与ng-view

# views/angular/index.html.erb 
<div class="container" ng-view> 
</div> 

我不会建议把ng-view in layouts/application,最好放在页面模板文件中。

现在默认的Rails页面应该消失了,你可以测试你的Angular应用程序。