填写这份《一分钟调查》,帮我们(开发组)做得更好!去填写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 更改时为监听器触发的回调函数。

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

err (e: Error) => void

发生错误时触发的回调函数。

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:[email protected]: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.