目录搜索
AngularJS API Referenceautoauto/serviceauto/service/$injectorauto/service/$providengng/directiveng/directive/ang/directive/formng/directive/inputng/directive/input[checkbox]ng/directive/input[date]ng/directive/input[dateTimeLocal]ng/directive/input[email]ng/directive/input[month]ng/directive/input[number]ng/directive/input[radio]ng/directive/input[text]ng/directive/input[time]ng/directive/input[url]ng/directive/input[week]ng/directive/ngAppng/directive/ngBindng/directive/ngBindHtmlng/directive/ngBindTemplateng/directive/ngBlurng/directive/ngChangeng/directive/ngCheckedng/directive/ngClassng/directive/ngClassEvenng/directive/ngClassOddng/directive/ngClickng/directive/ngCloakng/directive/ngControllerng/directive/ngCopyng/directive/ngCspng/directive/ngCutng/directive/ngDblclickng/directive/ngDisabledng/directive/ngFocusng/directive/ngFormng/directive/ngHideng/directive/ngHrefng/directive/ngIfng/directive/ngIncludeng/directive/ngInitng/directive/ngKeydownng/directive/ngKeypressng/directive/ngKeyupng/directive/ngListng/directive/ngModelng/directive/ngModelOptionsng/directive/ngMousedownng/directive/ngMouseenterng/directive/ngMouseleaveng/directive/ngMousemoveng/directive/ngMouseoverng/directive/ngMouseupng/directive/ngNonBindableng/directive/ngOpenng/directive/ngPasteng/directive/ngPluralizeng/directive/ngReadonlyng/directive/ngRepeatng/directive/ngSelectedng/directive/ngShowng/directive/ngSrcng/directive/ngSrcsetng/directive/ngStyleng/directive/ngSubmitng/directive/ngSwitchng/directive/ngTranscludeng/directive/ngValueng/directive/scriptng/directive/selectng/directive/textareang/filterng/filter/currencyng/filter/dateng/filter/filterng/filter/jsonng/filter/limitTong/filter/lowercaseng/filter/numberng/filter/orderByng/filter/uppercaseng/functionng/function/angular.bindng/function/angular.bootstrapng/function/angular.copyng/function/angular.elementng/function/angular.equalsng/function/angular.extendng/function/angular.forEachng/function/angular.fromJsonng/function/angular.identityng/function/angular.injectorng/function/angular.isArrayng/function/angular.isDateng/function/angular.isDefinedng/function/angular.isElementng/function/angular.isFunctionng/function/angular.isNumberng/function/angular.isObjectng/function/angular.isStringng/function/angular.isUndefinedng/function/angular.lowercaseng/function/angular.moduleng/function/angular.noopng/function/angular.toJsonng/function/angular.uppercaseng/objectng/object/angular.versionng/providerng/provider/$animateProviderng/provider/$compileProviderng/provider/$controllerProviderng/provider/$filterProviderng/provider/$httpProviderng/provider/$interpolateProviderng/provider/$locationProviderng/provider/$logProviderng/provider/$parseProviderng/provider/$rootScopeProviderng/provider/$sceDelegateProviderng/provider/$sceProviderng/serviceng/service/$anchorScrollng/service/$animateng/service/$cacheFactoryng/service/$compileng/service/$controllerng/service/$documentng/service/$exceptionHandlerng/service/$filterng/service/$httpng/service/$httpBackendng/service/$interpolateng/service/$intervalng/service/$localeng/service/$locationng/service/$logng/service/$parseng/service/$qng/service/$rootElementng/service/$rootScopeng/service/$sceng/service/$sceDelegateng/service/$templateCacheng/service/$timeoutng/service/$windowng/typeng/type/$cacheFactory.Cacheng/type/$compile.directive.Attributesng/type/$rootScope.Scopeng/type/angular.Moduleng/type/form.FormControllerng/type/ngModel.NgModelControllerngAnimatengAnimate/providerngAnimate/provider/$animateProviderngAnimate/servicengAnimate/service/$animatengCookiesngCookies/servicengCookies/service/$cookiesngCookies/service/$cookieStorengMessagesngMessages/directivengMessages/directive/ngMessagengMessages/directive/ngMessagesngMockngMock/functionngMock/function/angular.mock.dumpngMock/function/angular.mock.injectngMock/function/angular.mock.modulengMock/objectngMock/object/angular.mockngMock/providerngMock/provider/$exceptionHandlerProviderngMock/servicengMock/service/$exceptionHandlerngMock/service/$httpBackendngMock/service/$intervalngMock/service/$logngMock/service/$timeoutngMock/typengMock/type/angular.mock.TzDatengMockE2EngMockE2E/servicengMockE2E/service/$httpBackendngResourcengResource/servicengResource/service/$resourcengRoutengRoute/directivengRoute/directive/ngViewngRoute/providerngRoute/provider/$routeProviderngRoute/servicengRoute/service/$routengRoute/service/$routeParamsngSanitizengSanitize/filterngSanitize/filter/linkyngSanitize/servicengSanitize/service/$sanitizengTouchngTouch/directivengTouch/directive/ngClickngTouch/directive/ngSwipeLeftngTouch/directive/ngSwipeRightngTouch/servicengTouch/service/$swipe
文字

AngularJS: API: ng/service/$location


$location

  1. - $locationProvider
  2. - service in module ng

The $location service parses the URL in the browser address bar (based on the window.location) and makes the URL available to your application. Changes to the URL in the address bar are reflected into $location service and changes to $location are reflected into the browser address bar.

The $location service:

  • Exposes the current URL in the browser address bar, so you can
    • Watch and observe the URL.
    • Change the URL.
  • Synchronizes the URL with the browser when the user
    • Changes the address bar.
    • Clicks the back or forward button (or clicks a History link).
    • Clicks on a link.
  • Represents the URL object as a set of methods (protocol, host, port, path, search, hash).

For more information see Developer Guide: Using $location

依赖

  • $rootElement

方法

  • absUrl();

    This method is getter only.

    Return full url representation with all segments encoded according to rules specified in RFC 3986.

    返回值

    string

    full url

  • url([url], [replace]);

    This method is getter / setter.

    Return url (如: /path?a=b#hash) when called without any parameter.

    Change path, search and hash, when called with parameter and return $location.

    参数

    参数 类型 详述
    url
    (可选)
    string

    New url without base prefix (如: /path?a=b#hash)

    replace
    (可选)
    string

    The path that will be changed

    返回值

    string

    url

  • protocol();

    This method is getter only.

    Return protocol of current url.

    返回值

    string

    protocol of current url

  • host();

    This method is getter only.

    Return host of current url.

    返回值

    string

    host of current url.

  • port();

    This method is getter only.

    Return port of current url.

    返回值

    Number

    port

  • path([path]);

    This method is getter / setter.

    Return path of current url when called without any parameter.

    Change path when called with parameter and return $location.

    注意: Path should always begin with forward slash (/), this method will add the forward slash if it is missing.

    参数

    参数 类型 详述
    path
    (可选)
    string

    New path

    返回值

    string

    path

  • search(search, [paramValue]);

    This method is getter / setter.

    Return search part (as object) of current url when called without any parameter.

    Change search part when called with parameter and return $location.

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

    参数

    参数 类型 详述
    search stringObject.<string>Object.<Array.<string>>

    New search params - string or hash object.

    When called with a single argument the method acts as a setter, setting the search component of $location to the specified value.

    If the argument is a hash object containing an array of values, these values will be encoded as duplicate search parameters in the url.

    paramValue
    (可选)
    stringArray.<string>boolean

    If search is a string, then paramValue will override only a single search property.

    If paramValue is an array, it will override the property of the search component of $location specified via the first argument.

    If paramValue is null, the property specified via the first argument will be deleted.

    If paramValue is true, the property specified via the first argument will be added with no value nor trailing equal sign.

    返回值

    Object

    If called with no arguments returns the parsed search object. If called with one or more arguments returns $location object itself.

  • hash([hash]);

    This method is getter / setter.

    Return hash fragment when called without any parameter.

    Change hash fragment when called with parameter and return $location.

    参数

    参数 类型 详述
    hash
    (可选)
    string

    New hash fragment

    返回值

    string

    hash

  • replace();

    If called, all changes to $location during current $digest will be replacing current history record, instead of adding new one.

事件

  • $locationChangeStart

    Broadcasted before a URL will change. This change can be prevented by calling preventDefault method of the event. See $rootScope.Scope for more details about event object. Upon successful change $locationChangeSuccess is fired.

    类型:

    broadcast

    目标:

    root scope
  • $locationChangeSuccess

    Broadcasted after a URL was changed.

    类型:

    broadcast

    目标:

    root scope

上一篇:下一篇: