angularJS如何将用户访问的URL路由? [英] angularJS how to user accessible url routing?

查看:126
本文介绍了angularJS如何将用户访问的URL路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

像下面的基本的路由方法,

The basic routing method like below,

angular.module('myApp', ['myApp.filters', 'myApp.services', 'myApp.directives', 'myApp.controllers']).
 config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
  $routeProvider.when('/view1', {templateUrl: 'partials/partial1.html', controller: 'MyCtrl1'});
  $routeProvider.when('/view2', {templateUrl: 'partials/partial2.html', controller: 'MyCtrl2'});
  $routeProvider.otherwise({redirectTo: '/view1'});
  $locationProvider.html5Mode(true);
}]);

我想使用户可以通过键入URL来访问视图1:浏览器的//主机/厂景。但这不是工作..

I want make user can access view1 by typing url "://host/view1" on browser. but this is not work..

通过使用$ routeProvider和$ locationProvider,如果用户点击的href = /厂景这个环节,那么
页排到主机/厂景。

By using $routeProvider and $locationProvider, if user click "href=/view1" this link, then page routed host/view1.

但是,如果用户直接访问该URL://主机/视图1,它发射出404错误。
://主机/#/视图1

But If user access directly to the url "://host/view1" it emit 404 error. To make user can access /view1 directly by url, I have to remove html5Mode then, user can access by "://host/#/view1"

但我怎么能删除#符号!?不只是html5mode,该方法用户可以通过键入该URL直接访问该页面。

but How can I remove the # symbol!? not only just html5mode, the method user can access the page directly by typing that url.

推荐答案

感谢。耀西

我创建的DocumentRoot文件夹的.htaccess文件,添加以下

I create .htaccess file on DocumentRoot folder and add below

RewriteEngine   On
RewriteBase     /
RewriteCond     %{REQUEST_URI} !^(/index\.php|/img|/js|/css|/robots\.txt|/favicon\.ico)
RewriteCond     %{REQUEST_FILENAME} !-f
RewriteCond     %{REQUEST_FILENAME} !-d
RewriteRule     .               /index.html              [L]

到.htaccess文件。

into .htaccess file.

PS。

RewriteCond     %{REQUEST_FILENAME} !-f
RewriteCond     %{REQUEST_FILENAME} !-d

以上两行意味着,如果在浏览器中与指定名称的文件不存在,或者在浏览器中的目录不存在,则PROCEDE到重写规则如下

above two lines means if the file with the specified name in the browser doesn't exist, or the directory in the browser doesn't exist then procede to the rewrite rule below

这篇关于angularJS如何将用户访问的URL路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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