【问题标题】:Navigating to non-default state with ui router使用 ui 路由器导航到非默认状态
【发布时间】:2015-05-12 17:26:26
【问题描述】:

这几天我一直在尝试解决这个问题,我在互联网上阅读的所有内容都表明它应该可以正常工作。只要我在应用程序中,我的应用程序路由就可以正常工作,但是当我尝试复制和粘贴 URL 时,它会丢失其状态并重定向到主页。

* 编辑 我开始认为这是因为 app.yaml 正在处理“无论如何都提供 index.html”,似乎无论 app.yaml 做什么都会剥离信息,这就是为什么 $state 的 url 为“” 编辑 *

我将我的服务器配置为返回 index.html,无论 url 是什么,因此它会使用我粘贴在浏览器栏中的 url 进入路由逻辑。当我在注入 $state 和 $stateParams 的运行块中放置断点时,它显示当前 url 是“”,当它使用我的路由到达配置块时,它会转到 .otherwise('/') 并重定向我到应用程序的开头。

app.run([
    '$rootScope', '$state', '$stateParams', function($rootScope, $state, $stateParams){
        $rootScope.$state = $state;
        $rootScope.$stateParams = $stateParams;
        console.log($rootScope);
}])

.config([
'$stateProvider', '$urlRouterProvider', '$locationProvider', function($stateProvider, $urlRouterProvider, $locationProvider){
    $locationProvider.html5Mode(true);
    $urlRouterProvider.otherwise('/');

    $stateProvider
        .state('home', {
            url: '/',
            templateUrl: 'app/layout/home.html',
            controller: function($stateParams){
                debugger;
            }
        })
        .state('other', {
             url: '/{abc:[a|b|c|d]}',
             templateUrl: 'app/layout/other.html'
         });
}]);

这是我的 app.yaml

application: app
version: 1
runtime: python27
api_version: 1
threadsafe: no

handlers:
- url: /(.*\.(gif|png|jpg|ico|js|css|html))
  static_files: \1
  upload: (.*\.(gif|png|jpg|ico|js|css|html))

- url: /robots.txt
  static_files: robots.txt
  upload: robots.txt 

- url: .*
   static_files: index.html
   upload: index.html //I am relying on this as a fallback for any 404's, it seems to work

skip_files:
- ^(.*/)?#.*#$
- ^(.*/)?.*~$
- ^(.*/)?.*\.py[co]$
- ^(.*/)?.*/RCS/.*$
- ^(.*/)?\..*$
- node_modules/*
- grunt/*
- Gruntfile.js
- less/*
- lib/*

这是我的 main.py,它只是 google 给你的默认值

import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template

class MainHandler(webapp.RequestHandler):
  def get (self, q):
    if q is None:
      q = 'index.html'

    path = os.path.join (os.path.dirname (__file__), q)
    self.response.headers ['Content-Type'] = 'text/html'
    self.response.out.write (template.render (path, {}))

def main ():
  application = webapp.WSGIApplication ([('/(.*html)?', MainHandler)],     debug=True)
  util.run_wsgi_app (application)

if __name__ == '__main__':
  main ()

所以当我访问 localhost:8000 时,我就可以正常访问主页。我单击一个链接转到其他链接,最终到达 localhost:8000/a 或 /b 等。如果我将该链接复制并粘贴到另一个选项卡中,我最终到达 localhost:8000。我在 run 和 config 块中设置了断点,并且 url 到达那里时还没有改变,所以我 90% 确定它不是服务器问题,但我不知道它可能是什么。在我去 /a 之前我确实遇到过这个问题,它会返回一个错误,无法获取 /a。所以我修复了这个问题,因为服务索引一直在变化,这就是为什么我相当有信心我正确地设置了服务器。

据我从我所有的研究中可以看出,这应该只是在没有任何配置的情况下工作,除了在角度方面。就像我说的那样,我可以很好地在我的应用程序中导航,所以看起来我的状态设置正确。

除非我遗漏了什么,否则我发现的类似问题似乎只有 SO 问题不适用。 1 是关于我修复的发球索引问题,另一个是关于我认为不是的“/”。

How can I go directly to a state with ui-router without first navigating to index.html

Can't navigate to ui-router state with URL

【问题讨论】:

    标签: javascript angularjs google-app-engine angular-ui-router google-app-engine-python


    【解决方案1】:

    如果有人想知道问题是正则表达式格式混乱

    //this is wrong
    url: '/{abc:[a|b|c|d]}'
    
    //this is right
    url: '/{abc:a|b|c|d}'
    

    我认为它在内部运行良好,因为我将 ui-sref 绑定到状态,当我尝试复制和粘贴 url 时,它依赖于匹配 url。无论哪种方式,它最终都在工作

    【讨论】:

      猜你喜欢
      • 2016-09-22
      • 1970-01-01
      • 2015-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-16
      • 1970-01-01
      • 2014-09-18
      相关资源
      最近更新 更多