Skip to content

Location subscribe event does not fire on routing #10963

Closed

Description

I'm submitting a ... (check one with "x")

[X ] bug report => search github for a similar issue or PR before submitting
[ ] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior
I subscribe to URL change events via location.subscribe. The event never fires when I navigate via router-link clicks.

Expected/desired behavior
The event should fire.

Reproduction of the problem
Below code is from the root component that contains the router-outlet.

import {Location} from '@angular/common';
constructor(private location: Location) {
 location.subscribe((value) => {
            console.log("Location changed");
        });
}

What is the expected behavior?
I should see "Location changed" in the console log when routing.

What is the motivation / use case for changing the behavior?
It is used in Angulartics: angulartics/angulartics2#46

Please tell us about your environment:

  • Angular version: 2.0.0-rc.X
    2.0.0-rc.4
  • Browser: [all | Chrome XX | Firefox XX | IE XX | Safari XX | Mobile Chrome XX | Android X.X Web Browser | iOS XX Safari | iOS XX UIWebView | iOS XX WKWebView ]
    Chrome
  • Language: [all | TypeScript X.X | ES6/7 | ES5]
    TypeScript 1.8

Activity

  1. pkozlowski-opensource commented on Aug 20, 2016

    @pkozlowski-opensource
    Member

    Could you please try to reproduce this in plunker with RC5, latest router and without any 3rd party libraries. My experience with angulartics2 so far was rather ... well, not great... and had to do work-arounds in one of my projects for the exact scenario you are rising: https://github.com/ng-bootstrap/ng-bootstrap/blob/master/demo/src/app/shared/angulartics2.workaround.ts

    In any case, please provide a minimal reproduce scenario in rc5 so we are sure that we are dealing with the Angular issue here.

  2. michalstepien commented on Aug 21, 2016

    @michalstepien

    @pkozlowski-opensource I create plnkr. http://plnkr.co/edit/WVX3gQPCZRgn52quxQay?p=preview
    If click Heroes or details it doesn't work. If click back button in details it works (window.history.back();).

  3. michalstepien commented on Aug 21, 2016

    @michalstepien

    Perhaps is related with this: #10909

  4. JonnyBGod commented on Sep 4, 2016

    @JonnyBGod

    It looks like this problem persist in RC6.

    It seems to work in test environment, following test works:

    describe('router support', function() {
          var EventSpy: any;
    
          beforeEach(() => {
            TestBed.configureTestingModule({
              imports: [
                RouterTestingModule.withRoutes(RoutesConfig),
                TestModule
              ],
              providers: [
                { provide: Location, useClass: SpyLocation }
              ]
            });
    
            EventSpy = jasmine.createSpy('EventSpy');
          });
    
          it('should track pages on route v3 change',
            fakeAsync(inject([Router, Location],
              (router: Router, location: Location) => {
                fixture = createRootWithRouter(router, RootCmp);
                location.subscribe((x: any) => EventSpy(x.url));
                router.navigateByUrl('/abc');
                advance(fixture);
                router.navigate(['def']);
                advance(fixture);
                expect(EventSpy).toHaveBeenCalledWith('/abc');
                expect(EventSpy).toHaveBeenCalledWith('/def');
              })));
        });
    
  5. added
    needs reproductionThis issue needs a reproduction in order for the team to investigate further
    and removed
    needs reproductionThis issue needs a reproduction in order for the team to investigate further
    on Oct 4, 2016
  6. DzmitryShylovich commented on Nov 12, 2016

    @DzmitryShylovich
    Contributor

    can u provide a repro with the latest angular?

  7. added
    needs reproductionThis issue needs a reproduction in order for the team to investigate further
    and removed
    needs reproductionThis issue needs a reproduction in order for the team to investigate further
    on Nov 12, 2016
  8. DmitryEfimenko commented on Dec 14, 2017

    @DmitryEfimenko

    This is still an issue in latest Angular: http://plnkr.co/edit/oDpsUKlgWTe84bu2cJUY?p=preview

  9. alnvny commented on Jun 2, 2019

    @alnvny

    does this issue fixed? I am still facing this.

  10. kaito3desuyo commented on Jun 8, 2019

    @kaito3desuyo

    I have same issue

  11. samuelpetroline commented on Sep 2, 2019

    @samuelpetroline

    In the docs it says:

    Subscribes to the platform's popState events.

    I guess it's not a bug, but a misunderstanding. It should be more clear to what event it's subscribing

  12. angular-automatic-lock-bot commented on Oct 3, 2019

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  13. locked and limited conversation to collaborators on Oct 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs reproductionThis issue needs a reproduction in order for the team to investigate further

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions