当前位置:  开发笔记 > 编程语言 > 正文

如何在Rails/Angularjs html5模式中删除重定向

如何解决《如何在Rails/Angularjshtml5模式中删除重定向》经验,为你挑选了1个好方法。

在使用带有rails的html 5模式时,如何在每次用户刷新angularjs页面时避免性能下降?我正在使用Angularjs html5模式Ruby on Rails路由重定向.使用我的设置,如果我http://localhost:3000/users立即访问该网址http://localhost:3000/?goto=users,然后重定向到http://localhost:3000/users.使用html5模式时,我该怎么做才能避免这种性质的重定向?

Rails路由
myApp::Application.routes.draw do
 namespace :api, defaults: {format: :json} do
   namespace :v1 do
    resources :users
   end
 end

# Re-route all angular requests to the angular router
get "/*path" => redirect("/?goto=%{path}")
root to: 'application#home'

结束

角度路线
myApp.config(function($routeProvider, $locationProvider, $httpProvider) {
    $httpProvider.defaults.headers.common['X-CSRF-Token'] = 
    $('meta[name=csrf-token]').attr('content');
    $routeProvider.
        when('/', {
            templateUrl: '../assets/mainIndex.html',            
            controller: 'MainController',
            redirectTo:
                function(current, path, search){
                  if(search.goto){
                    // if we were passed in a search param, and it has a path
                    // to redirect to, then redirect to that path
                    return "/" + search.goto;
                  }
                  else{
                    // else just redirect back to this location
                    // angular is smart enough to only do this once.
                    return "/";
                  }
                }
        }).when('/users', {
            templateUrl: '../assets/users/userIndex.html',          
            controller: 'UsersController'
        }).otherwise({
            redirectTo:'/'
        });
    $locationProvider.html5Mode(true);
});

以上是来自这个博客:http://omarriott.com/aux/angularjs-html5-routing-rails/



1> Spiritual In..:

只需使用此rails路线:

  # Route all valid requests to AngularJS
  root to: 'application#home'
  get "*path" => "application#home"

和rails action_controller:

  # We'll just use this as a launch point for our App
  def home
    # Render just the layout since this application is Angular driven
    # our layout/application has all the angular logic and our controllers
    # have no views for themselves. We just need a place to launch from
    # and this happens to be it. So we have no view (thus :nothing => true)
    # but we still want the layout (since it has the App bootstrap code)
    render :layout => 'application', :nothing => true
  end

现在不需要处理angularjs路由器的重定向.只需为您的网页制作普通路线即可.

推荐阅读
135369一生真爱_890
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有