angularJS: $location 服務

來源:互聯網
上載者:User

標籤:類型   javascrip   連接埠   跳轉   url   nbsp   target   host   記錄   

記錄下今天的學習筆記:

$location服務:

  1. 解析地址欄的URL,讓使用者可以訪問應用當前路徑所對應的路由。
  2. 可修改路徑、處理各種形式導航。

$location服務對javascript中的window.location對象的API進行了更優雅的封裝,並且和angularJS整合在一起。

使用$location服務的最佳情境是:當應用需要在內部進行跳轉時候。如:使用者註冊、修改、或登入後進行的跳轉。

$location服務並不重新整理整個頁面。重新整理整個頁面使用:$window.location對象(window.location的一個介面)

1. path() - 擷取頁面當前路徑:

$location.path();  //返回當前路徑

$location.path(‘/‘); //修改路徑為‘/‘路由,並跳轉

path()方法直接和H5的曆史API進行互動,所以使用者可通過點擊後退按鈕退回到上個頁面。

2. replace() - 禁止使用者跳轉後點擊後退按鈕。(如使用者登入後)

$location.path(‘/home‘);

$location.replace();

或者:

$location.path(‘/home‘).replace();

3. absUrl() - 擷取編碼後的完整URL

$location.absUrl();

4. hash() - 擷取URL中的hash片段

$location.hash(); // 返回當前的hash片段

5. host() - 擷取URL中的主機

$location.host();

6. port() - 擷取URL中的連接埠號碼

$location.port();

7. protocol() - 擷取URL中的協議

$location.protocol();

8. search() - 擷取URL中的查詢串

$location.search();

這個方法可傳入新的查詢參數,用來修改URL中的查詢串部分:

//用對象設定查詢:

$location.search({name: ‘Ari‘, username: ‘auser‘});

//用字串設定查詢:

$location.search(‘name=Air&username=auser‘);

search方法可接收兩個選擇性參數:

  • search(可選,字串或對象) - 代表新的查詢參數。hash對象的值可以是數組
  • paramValue(可選,字串) - 如果search參數的類型是字串,那麼paramValue會作為該參數的值覆蓋URL當中對應的值。如果paramValue的值是null,對應的參數會被移除。

9. url() - 擷取當前的URL:

$location.url();

調用url()方法時如果傳了參數,會設定並修改當前的URL,同時修改URL中的路徑、查詢串和hash,並返回$location。

//設定新的URL

$location.url(‘/home?name=Ari#hashthing‘);

方法可接收兩個參數:

url(可選,字串) - 新的URL的基礎的首碼。

replace(可選,字串) - 想要修改成的路徑。

angularJS: $location 服務

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.