“未找到”当routeProvider拥有ID [英] 'Not Found' when routeProvider has id

查看:185
本文介绍了“未找到”当routeProvider拥有ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设置我的routeProvider这样:

  routeProvider.when('/',{templateUrl:意见/ index.html的,控制器:'IndexCtrl'});
  // $ routeProvider.when('/阿亚特/:古兰经/:从/:为{templateUrl:意见/ quran.html',控制器:'AyatCtrl'});
  $ routeProvider.when('/:ID /:范围是多少?',{templateUrl:意见/ quran.html',控制器:'QuranCtrl'});
  $ routeProvider.when('/搜索?',{templateUrl:意见/ search.html',控制器:'SearchCtrl'});
  $ routeProvider.when('/约',{templateUrl:意见/ about.html'});
  $ routeProvider.when('/接触',{templateUrl:意见/ contact.html'});
  $ routeProvider.when('/对不起',{templateUrl:意见/ sorry.html'});
  $ routeProvider.otherwise({redirectTo:'/'});
  $ locationProvider.html5Mode(真);
})

每当我刷新页面,我得到未找到

我没有遇到这个问题时previously我不得不

  $ routeProvider.when('/:扩展/:ID /:范围是多少?',{templateUrl:意见/ quran.html',控制器:'QuranCtrl'});
$ locationProvider.html5Mode(假);


解决方案

在种子项目的package.json文件指定NPM刚刚开始运行的HTTP服务器,它是一个节点模块。它似乎没有正确处理html5mode,因为为了来处理,它需要重定向是不是为特定的文件到你的index.html文件的所有请求。您可以自定义节点服务器而不是为pretty简单,只需提供正确的文件,并允许刷新浏览器,应有尽有。

看看这个简单的例子

补充说,在您的应用程序(同一水平app文件夹),然后根通过改变脚本的开始属性修改的package.json开始:节点server.js

这确定是否将请求路由到index.html或不是本方法的方法

 函数isSpecificFile(路径){
  VAR正则表达式= /^\\/(bower_components|css|img|js|partials)\\/.*/;
  返回regex.test(路径);
}

如果你有一个像本地主机的要求:8000 /富,需要在角度做,这样的index.html将被载入和角度将加载特定的文件与窗体/js/controllers/foo_ctrl.js,该服务器将无法路由的index.html。

的请求

我会找任何东西像生产虽然一个更强大的解决方案。

I set my routeProvider as such:

routeProvider.when('/', {templateUrl: 'views/index.html', controller: 'IndexCtrl'});
  // $routeProvider.when('/ayat/:surah/:from/:to', {templateUrl: 'views/quran.html', controller: 'AyatCtrl'});
  $routeProvider.when('/:id/:range?', {templateUrl: 'views/quran.html', controller: 'QuranCtrl'});
  $routeProvider.when('/search?', {templateUrl: 'views/search.html', controller: 'SearchCtrl'});
  $routeProvider.when('/about', {templateUrl: 'views/about.html'});
  $routeProvider.when('/contact', {templateUrl: 'views/contact.html'});
  $routeProvider.when('/sorry', {templateUrl: 'views/sorry.html'});
  $routeProvider.otherwise({redirectTo: '/'});
  $locationProvider.html5Mode(true);
})

Whenever I refresh the page, I get 'Not Found'

I didn't run into this problem when previously I had

$routeProvider.when('/:extension/:id/:range?', {templateUrl: 'views/quran.html', controller: 'QuranCtrl'});
$locationProvider.html5Mode(false);

解决方案

the package.json file in the seed project specifies that npm start just runs http-server, which is a node module. It didn't seem to handle html5mode correctly, because in order to handle that, it needs to redirect all requests that aren't for a specific file to your index.html file. You can make a custom node server instead that is pretty simple and just serves the correct files, and allows for refreshing your browser and everything.

Check out this simple example

add that at the root of your application (same level as app folder) and then change package.json by changing the start property of scripts to "start": "node server.js"

the method that determines whether or not to route the request to index.html or not is this one

function isSpecificFile(path){
  var regex = /^\/(bower_components|css|img|js|partials)\/.*/;
  return regex.test(path);
}

If you have a request like localhost:8000/foo, that needs to be done in angular, so index.html will be loaded and angular will load the specific files with requests of the form '/js/controllers/foo_ctrl.js', which the server won't route to index.html.

I would look for a more robust solution for anything production like though.

这篇关于“未找到”当routeProvider拥有ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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