填寫這份《一分鐘調查》,幫我們(開發組)做得更好!去填寫Home

$locationShim

位置服務,提供對 AngularJS 中提供的 $location 服務的直接替代品。

Location service that provides a drop-in replacement for the $location service provided in AngularJS.

      
      class $locationShim {
  constructor($injector: any, location: Location, platformLocation: PlatformLocation, urlCodec: UrlCodec, locationStrategy: LocationStrategy)
  onChange(fn: (url: string, state: unknown, oldUrl: string, oldState: unknown) => void, err: (e: Error) => void = (e: Error) => { })
  $$parse(url: string)
  $$parseLinkUrl(url: string, relHref?: string): boolean
  absUrl(): string
  url(url?: string): string | this
  protocol(): string
  host(): string
  port(): number | null
  path(path?: string | number): string | this
  search(search?: string | number | { [key: string]: unknown; }, paramValue?: string | number | boolean | string[]): {...}
  hash(hash?: string | number): string | this
  replace(): this
  state(state?: unknown): unknown | this
}
    

參見

建構函式

      
      constructor($injector: any, location: Location, platformLocation: PlatformLocation, urlCodec: UrlCodec, locationStrategy: LocationStrategy)
    
引數
$injector any
location Location
platformLocation PlatformLocation
urlCodec UrlCodec
locationStrategy LocationStrategy

方法

註冊對 URL 更改的監聽器。該 API 用於捕獲 AngularJS 框架執行的更新。$locationChangeStart$locationChangeSuccess 事件的子集,這些事件在 AngularJS 更新其內部參考的瀏覽器 URL 版本時觸發。

Registers listeners for URL changes. This API is used to catch updates performed by the AngularJS framework. These changes are a subset of the $locationChangeStart and $locationChangeSuccess events which fire when AngularJS updates its internally-referenced version of the browser URL.

      
      onChange(fn: (url: string, state: unknown, oldUrl: string, oldState: unknown) => void, err: (e: Error) => void = (e: Error) => { })
    
引數
fn (url: string, state: unknown, oldUrl: string, oldState: unknown) => void

URL 更改時為監聽器觸發的回呼(Callback)函式。

The callback function that is triggered for the listener when the URL changes.

err (e: Error) => void

發生錯誤時觸發的回呼(Callback)函式。

The callback function that is triggered when an error occurs.

可選. 預設值是 `(e: Error) => { }`.

$locationChange 事件有可能發生,但瀏覽器的 URL(window.location)保持不變。僅當 AngularJS 實際上更新瀏覽器 URL(window.location)時,才會觸發此 onChange

It's possible for $locationChange events to happen, but for the browser URL (window.location) to remain unchanged. This onChange callback will fire only when AngularJS actually updates the browser URL (window.location).

解析此 URL,並將當前 URL 設定為解析結果。

Parses the provided URL, and sets the current URL to the parsed result.

      
      $$parse(url: string)
    
引數
url string

URL 字串。

The URL string.

解析提供的 URL 及其相對 URL。

Parses the provided URL and its relative URL.

      
      $$parseLinkUrl(url: string, relHref?: string): boolean
    
引數
url string

完整的 URL 字串。

The full URL string.

relHref string

相對於完整 URL 字串的 URL 字串。

A URL string relative to the full URL string.

可選. 預設值是 `undefined`.
返回值

boolean

檢索完整的 URL 表示形式,其中包含根據 RFC 3986 中 指定的規則編碼過的所有段。

Retrieves the full URL representation with all segments encoded according to rules specified in RFC 3986.

      
      absUrl(): string
    
引數

沒有引數。

返回值

string

      
      // given URL http://example.com/#/some/path?foo=bar&baz=xoxo
let absUrl = $location.absUrl();
// => "http://example.com/#/some/path?foo=bar&baz=xoxo"
    

檢索當前 URL,或設定新 URL。設定 URL 時,更改路徑、搜尋和雜湊,並返回對其自身實例的參考。

Retrieves the current URL, or sets a new URL. When setting a URL, changes the path, search, and hash, and returns a reference to its own instance.

      
      url(): string
    
引數

沒有引數。

返回值

string

      
      url(url: string): this
    
引數
url string
返回值

this

      
      // given URL http://example.com/#/some/path?foo=bar&baz=xoxo
let url = $location.url();
// => "/some/path?foo=bar&baz=xoxo"
    

檢索當前 URL 的協議。

Retrieves the protocol of the current URL.

      
      protocol(): string
    
引數

沒有引數。

返回值

string

      
      // given URL http://example.com/#/some/path?foo=bar&baz=xoxo
let protocol = $location.protocol();
// => "http"
    

檢索當前 URL 的協議。

Retrieves the protocol of the current URL.

      
      host(): string
    
引數

沒有引數。

返回值

string

與非 AngularJS 版本不同,其 location.host 會返回 hostname:port ,而這裡會返回 hostname 部分。

In contrast to the non-AngularJS version location.host which returns hostname:port, this returns the hostname portion only.

      
      // given URL http://example.com/#/some/path?foo=bar&baz=xoxo
let host = $location.host();
// => "example.com"

// given URL http://user:password@example.com:8080/#/some/path?foo=bar&baz=xoxo
host = $location.host();
// => "example.com"
host = location.host;
// => "example.com:8080"
    

檢索當前 URL 的埠。

Retrieves the port of the current URL.

      
      port(): number | null
    
引數

沒有引數。

返回值

number | null

      
      // given URL http://example.com/#/some/path?foo=bar&baz=xoxo
let port = $location.port();
// => 80
    

檢索當前 URL 的路徑,或更改路徑並返回對其自身實例的參考。

Retrieves the path of the current URL, or changes the path and returns a reference to its own instance.

      
      path(): string
    
引數

沒有引數。

返回值

string

      
      path(path: string | number): this
    
引數
path string | number
返回值

this

路徑應始終以正斜槓(/)開頭。如果缺少此斜槓,則此方法將新增它。

Paths should always begin with forward slash (/). This method adds the forward slash if it is missing.

      
      // given URL http://example.com/#/some/path?foo=bar&baz=xoxo
let path = $location.path();
// => "/some/path"
    

3 個過載形式...

顯示所有 隱藏所有 expand_more
Overload #1

檢索當前 URL 的搜尋引數的對映,或更改搜尋部分並返回對其自身實例的參考。

Retrieves a map of the search parameters of the current URL, or changes a search part and returns a reference to its own instance.

      
      search(): {
    [key: string]: unknown;
}
    
引數

沒有引數。

返回值

`{

當前 URL 的已解析 search 物件,或更改後的 search 物件。

}: The parsed searchobject of the current URL, or the changedsearch` object.


Overload #2
      
      search(search: string | number | { [key: string]: unknown; }): this
    
引數
search string | number | { [key: string]: unknown; }
返回值

this


Overload #3
      
      search(search: string | number | { [key: string]: unknown; }, paramValue: string | number | boolean | string[]): this
    
引數
search string | number | { [key: string]: unknown; }
paramValue string | number | boolean | string[]
返回值

this

      
      // given URL http://example.com/#/some/path?foo=bar&baz=xoxo
let searchObject = $location.search();
// => {foo: 'bar', baz: 'xoxo'}

// set foo to 'yipee'
$location.search('foo', 'yipee');
// $location.search() => {foo: 'yipee', baz: 'xoxo'}
    

檢索當前雜湊片段,或更改雜湊片段並返回對其自身實例的參考。

Retrieves the current hash fragment, or changes the hash fragment and returns a reference to its own instance.

      
      hash(): string
    
引數

沒有引數。

返回值

string

      
      hash(hash: string | number): this
    
引數
hash string | number
返回值

this

      
      // given URL http://example.com/#/some/path?foo=bar&baz=xoxo#hashValue
let hash = $location.hash();
// => "hashValue"
    

當前 $digest 期間對 $location 更改將替換當前歷史記錄,而不是新增新的記錄。

Changes to $location during the current $digest will replace the current history record, instead of adding a new one.

      
      replace(): this
    
引數

沒有引數。

返回值

this

當不帶任何引數呼叫時將檢索歷史狀態物件。

Retrieves the history state object when called without any parameter.

      
      state(): unknown
    
引數

沒有引數。

返回值

unknown

      
      state(state: unknown): this
    
引數
state unknown
返回值

this

使用一個引數呼叫時將更改歷史狀態物件,並返回 $location 。狀態物件隨後傳遞給 pushStatereplaceState

Change the history state object when called with one parameter and return $location. The state object is later passed to pushState or replaceState.

僅在 HTML5 模式下以及在支援 HTML5 History API 方法(例如 pushStatereplaceState)的瀏覽器中才支援此方法。如果你需要支援較舊的瀏覽器(例如 Android <4.0),請不要使用此方法。

This method is supported only in HTML5 mode and only in browsers supporting the HTML5 History API methods such as pushState and replaceState. If you need to support older browsers (like Android < 4.0), don't use this method.