簡體   English   中英

角路由無法正常工作

[英]Angular routing not working correctly

因此,我嘗試在Ruby on Rails應用程序中嘗試Angular路由。

問題是,當我着陸時,假設為'/' ,並且有一個角度路由,即當我着陸時,它應該將模板加載到所需的目錄中,然后我的Rails服務器會拋出該模板錯誤:

Started GET "/templates/index.html.erb" for 127.0.0.1 at 2014-12-02 18:03:39 +0200
ActionController::RoutingError (No route matches [GET] "/templates/index.html.erb"):

因此,基本上,似乎它沒有嘗試從文件夾加載我的文件,而是嘗試將我告訴角度路由的路徑放入URL,然后發出請求?

因為如果我用/users替換templateUrl: :,這是我的應用程序中用html響應的實際路由,那么它將有效地路由到它。 但這不是我想要的行為。

我希望Angular將ng-view中的html模板替換為輸入調用路由時指定的模板。

這是我的角度路由代碼:

App = angular.module("App", ['ngRoute', 'growlNotifications'])

    App.config ($httpProvider) ->
        authToken = $("meta[name=\"csrf-token\"]").attr("content")
        $httpProvider.defaults.headers.common["X-CSRF-TOKEN"] = authToken

    App.config ($routeProvider, $locationProvider) ->
        $locationProvider.html5Mode true
        $routeProvider
            .when '/',
                templateUrl: 'templates/index.html.erb',
                controller: 'VisitorsCtrl'
            .when '/users',
                templateUrl: '../../views/users/index.html.erb',
                controller: 'UsersCtrl'
            .otherwise redirectTo: "/"

另外,在我的application.html.erb我將base href設置為'/'

我的模板的當前位置如下: app/assets/javascripts/angular/templates ,我的路由在angular文件夾中定義

這可能是什么問題?

  1. 您想獲得ERB模板,您可能想將erb更改為僅html
  2. 您的模板位於錯誤的目錄中,您需要使用正確的路徑,例如“ /assets/angular/templates/index.html”或將模板放入公共目錄“ / public / templates”中
  3. 如果您仍然想使用ERB來呈現模板,則需要一些解決方案,將這些模板與temp路由到url(/templates/sth.html)上進行路由(config / routes.rb),但我想這是一個非常丑陋的解決方案。
  4. 我認為最適合您的解決方案是遵循本教程 :)它們將模板存儲在public / templates目錄中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM