如何纠正AngularJS在Rails的4组根路线? [英] How to correct set root route in Rails 4 for AngularJS?
问题描述
我有Rails的整合与AngularJS路由选择的烦恼。
I have a troubles with integrating Rails routing with AngularJS.
当我去本地主机:3000在浏览器中输入,它重定向我到本地主机:3000 /#/认证/ sign_in
When I go to "localhost:3000" in browser input, it redirect me to the "localhost:3000/#/auth/sign_in"
但'sign_in模板不会渲染。
but 'sign_in' template not render.
然后,二级如果我去了本地主机:3000 /#/,它重定向我的。本地主机:3000 /#/认证/ sign_in和渲染sign_in模板权
Then, secondary if I go to the "localhost:3000/#/", it redirect me to the "localhost:3000/#/auth/sign_in" and render 'sign_in' template right.
如何解决它,当我去?本地主机:3000然后渲染sign_in
How to fix it, when I go to "localhost:3000" then render 'sign_in' ?
路由器code在这里: http://pastie.org/8917505
Router code here: http://pastie.org/8917505
谢谢!
推荐答案
我已经找到了解决方案。
I have found the solution.
您需要启用的 HTML5模式作为AngularJS:
You need to enable HTML5 mode for AngularJS:
angular.module('app').config(['$routeProvider', '$locationProvider',
function ($routeProvider, $locationProvider) {
...
$locationProvider.html5Mode(true);
}]
)
您还需要添加<碱基GT;
HTML标签:
You also need to add the <base>
html tag:
<html>
<head>
<base href="/">
...
</head>
</html>
它修复了角的URL路径不正确的处理,当你preSS在浏览器中输入URL再次输入。没有这一点,最后的'域'将被重复:
It fixes Angular's incorrect processing of URL-path when you press Enter in the browser URL input again. Without this, the last 'domain' will be repeated:
本地主机:3000 /认证/ sign_in
localhost:3000/auth/sign_in
本地主机:3000 /认证/认证/ sign_in
localhost:3000/auth/auth/sign_in
...
您还需要编写服务器端正确的路由(在Rails)的所有SPA路线:
You also need to write correct route on server side (in Rails) for all SPA routes:
的config / routes.rb中
root 'application#main'
get '*path', to: 'application#main'
这篇关于如何纠正AngularJS在Rails的4组根路线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!