简体   繁体   English

角路由无法正常工作

[英]Angular routing not working correctly

So I am attempting to try out Angular routing in my Ruby on Rails application. 因此,我尝试在Ruby on Rails应用程序中尝试Angular路由。

The problem is that, when I land on a route, let's say '/' , and I have a angular routing that say's that when I land on it, it should load a template in my desired directory, then my Rails server will throw this error: 问题是,当我着陆时,假设为'/' ,并且有一个角度路由,即当我着陆时,它应该将模板加载到所需的目录中,然后我的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"):

So basically it seems that it does not attempt to load my file from the folder but instead it tries to put the path that I told the angular route to the URL, and then make a request? 因此,基本上,似乎它没有尝试从文件夹加载我的文件,而是尝试将我告诉角度路由的路径放入URL,然后发出请求?

Because if I replace the templateUrl: with /users , which is an actual route in my app that responds with html, then It will effectily route to it. 因为如果我用/users替换templateUrl: :,这是我的应用程序中用html响应的实际路由,那么它将有效地路由到它。 But that is not the behaviour I want. 但这不是我想要的行为。

I want Angular to replace the html template in my ng-view with the template specified when I enter the called route. 我希望Angular将ng-view中的html模板替换为输入调用路由时指定的模板。

Here is my code for angular routing: 这是我的角度路由代码:

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: "/"

Also, in my application.html.erb I have set the base href to '/' 另外,在我的application.html.erb我将base href设置为'/'

My template are located currently as so: app/assets/javascripts/angular/templates and my routing is defined in the angular folder 我的模板的当前位置如下: app/assets/javascripts/angular/templates ,我的路由在angular文件夹中定义

What could be the problem here? 这可能是什么问题?

  1. You want to get ERB templates, you probably want to change erb to only html 您想获得ERB模板,您可能想将erb更改为仅html
  2. Your templates are in wrong directory, for what you want you need to use correct path which si something like: "/assets/angular/templates/index.html" or put your templates into public directory "/public/templates" 您的模板位于错误的目录中,您需要使用正确的路径,例如“ /assets/angular/templates/index.html”或将模板放入公共目录“ / public / templates”中
  3. In case you want to still use ERB for rendering templates, you will need some solution with routing (config/routes.rb) this tempaltes to page controller bind on url: /templates/sth.html, but I guess it's quite ugly solution. 如果您仍然想使用ERB来呈现模板,则需要一些解决方案,将这些模板与temp路由到url(/templates/sth.html)上进行路由(config / routes.rb),但我想这是一个非常丑陋的解决方案。
  4. I think the best solution for you is follow this great tutorial :) They store templates in public/templates directory. 我认为最适合您的解决方案是遵循本教程 :)它们将模板存储在public / templates目录中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM