【问题标题】:Routing with Angular doesn't work使用 Angular 路由不起作用
【发布时间】:2014-10-23 10:30:44
【问题描述】:

在我的 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

【问题讨论】:

  • 如何将应用绑定到您的应用?

标签: angularjs ruby-on-rails-4


【解决方案1】:

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

您可以将其引导到文档中

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

或者在你的索引上绑定某人

<body ng-app="appName">

</body>

【讨论】:

    【解决方案2】:

    使用“#task”和“#watchlist”,你的时间是html5mode,使用$locationProvider设置。

    【讨论】:

    • 我之前尝试过html5mode,当它是假的和真的时,我只是不记得在链接中使用'#/'。
    猜你喜欢
    • 2018-02-22
    • 2018-06-17
    • 2017-12-12
    • 2014-05-05
    • 2017-11-04
    • 1970-01-01
    • 1970-01-01
    • 2019-02-01
    相关资源
    最近更新 更多