自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

AngularJS中的友好URL:移除URL中的#

開發(fā) 前端
這是一個在Angular應(yīng)用中獲得漂亮URL并刪除哈希標記的簡單方法。享受超潔凈、超快速的Angular應(yīng)用吧!

AngularJS 默認將會使用一個 # 號來對URL進行路由.

例如:

  • http://example.com/

  • http://example.com/#/about

  • http://example.com/#/contact

要獲得干凈的URL并將井號從URL中移除是很容易的.

完成兩件事情就行了.

  1. 配置 $locationProvider

  2. 設(shè)置我們的相對連接的起點路徑

$location 服務(wù)

在Angular中, $location服務(wù)會解析地址欄中的URL,并對你的應(yīng)用程序作出改變,反之亦然.

我強烈推薦通讀官方的 Angular $location 文檔 以對$location 服務(wù)及其所提供的特性有一個了解.

$locationProvider 和 html5 模式(html5Mode)

我們會使用 $locationProvider 模塊,并將html5Mode設(shè)置為true.

我們會在你定義Angular應(yīng)用程序并配置你的路由時做這些.

  1. angular.module('scotchy', [])  
  2.       
  3.     .config(function($routeProvider, $locationProvider) {  
  4.  
  5.         $routeProvider  
  6.             .when('/', {  
  7.                 templateUrl : 'partials/home.html',  
  8.                 controller : mainController  
  9.             })  
  10.             .when('/about', {  
  11.                 templateUrl : 'partials/about.html',  
  12.                 controller : mainController  
  13.             })  
  14.             .when('/contact', {  
  15.                 templateUrl : 'partials/contact.html',  
  16.                 controller : mainController  
  17.             });  
  18.       
  19.         // use the HTML5 History API  
  20.         $locationProvider.html5Mode(true);  
  21.     });  

什么是 HTML5 History API? 它是使用一個腳本來操作瀏覽器歷史的標準方法. 有了它就能在不刷新頁面的前提下讓 Angular 改變路由和頁面的URL. 更多的信息,這里有一篇蠻好的 HTML5 History API 文章.

為相對鏈接設(shè)置<base> 

為了在應(yīng)用程序各處使用相對鏈接,你將需要在你文檔的<head>里面設(shè)置一個<set>.

  1. <!doctype html> 
  2. <html> 
  3. <head> 
  4.     <meta charset="utf-8"> 
  5.  
  6.     <base href="/"> 
  7. </head> 

有大量的方法可以用來配置這個東西,而將HTML5Mode設(shè)置為true就會自動的解析相對鏈接了. 在我這兒這種方式總是能起效. 如果你應(yīng)用程序的根同url相比有所不同,例如 /my-base, 那就用那個作為你的起點路徑.

老瀏覽器的回調(diào)

$location服務(wù)對不支持HTML5瀏覽歷史API的瀏覽器將自動回調(diào)hashbang方法

一切的發(fā)生對你是透明的,你不需為此做任何配置。從Angular $location文檔中,你可以看到回調(diào)的方法已經(jīng)它是如何工作的。

hashbang_vs_regular_url

總結(jié)

這是一個在Angular應(yīng)用中獲得漂亮URL并刪除哈希標記的簡單方法。享受超潔凈、超快速的Angular應(yīng)用吧!

英文原文:Pretty URLs in AngularJS: Removing the #

譯文來自:http://www.oschina.net/translate/pretty-urls-in-angularjs-removing-the-hashtag

責(zé)任編輯:林師授 來源: 開源中國社區(qū) 編譯
相關(guān)推薦

2011-05-11 14:50:54

URL

2021-04-16 20:50:16

URL爬蟲參數(shù)

2010-10-25 10:13:16

ibmdwWebSphere

2023-02-01 14:08:53

JavaScriptURL安全

2009-07-31 09:58:20

URL映射ASP.NET

2021-08-30 23:47:28

URLQuery字段

2009-07-23 16:28:20

URL映射ASP.NET 2.0

2009-07-22 14:23:39

URL RewriteASP.NET

2009-01-04 13:27:10

URL RewriteIISASP.NET

2010-05-11 14:30:39

2009-10-26 15:55:43

URL Routing

2010-05-11 13:07:23

Mysql JDBC

2020-10-28 08:10:27

URL域名https

2009-09-24 09:26:22

ASP.NET MVC

2021-10-10 18:29:27

URL編碼Go

2009-07-24 11:20:43

ASP.NET MVC

2021-03-02 07:31:26

WebApiweb

2009-07-07 10:14:57

基于URL權(quán)限控制

2016-09-27 21:14:53

JavaURL

2022-06-30 12:43:53

Firefox隱私功能URL
點贊
收藏

51CTO技術(shù)棧公眾號