如何纠正AngularJS在Rails的4组根路线? [英] How to correct set root route in Rails 4 for AngularJS?

查看:108
本文介绍了如何纠正AngularJS在Rails的4组根路线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆