使用Angular进行路由不起作用

时间:2014-08-29 11:56:01

标签: angularjs ruby-on-rails-4

在我的应用程序rails with angular中,我创建目录/app/assets/javascripts/angular/templates来创建我的视图。

我的 route.js.coffe

app.config ($routeProvider) ->
  $routeProvider
  .when('/task', {
    templateUrl: 'templates/task/index.html'
    controller: 'TaskCtrl'
  })

我的index.slim

.container
  h2
    |What would you like to do?

  a href="#/watchlist"
    | Watchlist
  | &nbsp or &nbsp
  a href="#/task"
    | Task

  div class="row" ng-view=""

当我点击“任务”链接时,没有任何反应。我在控制台上看到以下消息:

  

在2014-08-29 08:49:17 -0300开始获取192.168.1.106的“/templates/task/index.html”     ActionController :: RoutingError(没有路由匹配[GET]“/templates/task/index.html”):

我的index.html:

<div ng-controller="TaskCtrl">
  <h2>teste</h2>
</div>

和我的route.rb

Rails.application.routes.draw do

  get 'home/index'

  scope :api, defaults: {format: :json} do
    resources :tasks
    resources :stocks
  end

  root 'home#index'

end

2 个答案:

答案 0 :(得分:1)

使用&#34; #task&#34;和&#34; #watchlist&#34;,你的html5mode是什么时候,使用$ locationProvider设置。

答案 1 :(得分:0)

确保将应用程序本身绑定到视图。

您可以将其引导至文档

angular.bootstrap(document, ['appname']); 

或者在索引上绑定某人

<body ng-app="appName">

</body>