Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

calling previous controller #27

Closed
naczu opened this issue Nov 14, 2013 · 10 comments
Closed

calling previous controller #27

naczu opened this issue Nov 14, 2013 · 10 comments

Comments

@naczu
Copy link

naczu commented Nov 14, 2013

Hi. This is really great lib. I started to use angular-route-segment for nested views. But I have a problem. When I route to another segment, it calls previous controller. Here is you can see my problem

http://plnkr.co/edit/VUZM1ts5GvLnC2nf6QLe?p=preview

I would be grateful if you have a solution for this.

http://stackoverflow.com/questions/19978291/angular-route-segment-calls-previous-controllers

@djds4rce
Copy link

djds4rce commented Feb 2, 2014

If iam not wrong this issue is because the app-view-segment chain isnt flushed before a route change instead it is incrementally substituted which causes this issue. It would be great it this was fixed, cause this causes a lot of nasty issues.

Edit: Just found a fix for it #7
Edit2: Sorry it is a unrelated issue.

@ghost
Copy link

ghost commented Feb 18, 2014

I am also getting the same problem .. here is my plunkr http://plnkr.co/edit/VPFOi7?p=preview

Please check console.logs to see the error

Here is the hierarchy

/listing ( cntrller :- listing_index )
-> /listing/myListing ( cntrller :- my_listing )
/promotion ( cntrller :- promotion_index )
-> /promotion/list ( cntrller :- promotion_list )

Initially the default url is /listing/myListing so it calls the following controllers ( listing_index,my_listing )

Now on clicking a link which takes to /promotion/list the following controllers are called ( promotion_index, my_listing, promotion_list )

why is my_listing getting called again ??

Please check console for the errors

@jamie-pate
Copy link

+1, this should not be happening!
A workaround I found was to use a guard in the controller.

Compare $route.current.$$route.segment === $routeSegment.name

if ($route.current.$$route.segment === $routeSegment.name) {
//do stuff that should only happen when the
// *correct* segment is being initialized
}

PS: angular-route-segment v1.2.0

@naczu
Copy link
Author

naczu commented Feb 23, 2014

@jamie-pate that controller should never fired.

@jamie-pate
Copy link

@naczu It is fired though :)

PS: this workaround only works for controllers that are used in the right-most segment.

EG: route: x.y.z <- code only works in controller for z

@jamie-pate
Copy link

Ok, so. I figured out the 'correct' workaround for my problem. The issue for me was caused by calling $location.path(newPath) inside the constructor for a controller in one of the routes.

I was also mixing $routeProvider and $routeSegmentProvider and angular-route-segment wasn't updating the $routeSegment properly when the route wasn't a segment!

The solution was to not mix $routeProvider and $routeSegmentProvider

@abhikmitra
Copy link

+1 .Suffering form the same issue . @jamie-pate do you have a plunkr to illustrate your solution ?

@jamie-pate
Copy link

If you have a plunkr to illustrate your problem, I might be able to fix it?

@ghost
Copy link

ghost commented Apr 4, 2014

Hi @jamie-pate , will this do :- http://plnkr.co/edit/VPFOi7?p=preview

@jamie-pate
Copy link

That plunkr seems to work fine (no error messages)

It seems like $routeProvider is needed for redirects? I needed dynamic redirects so I ended up having to create a controller that did the job

   $routeSegmentProvider
     .when('/original/path', 'desired.segment_redirect')
     .when('/desired/segment', 'desired.segment')
     .segment('desired.segment_redirect', {
        template: '',
        controller: 'Redirect'
     })
     .segment('desired', {...})
     .within()
        .segment('segment', { ...})

    module.controller('Redirect',Redirect);
    function Redirect($location, persistentStorage) {
        var type = persistentStorage.get(type_key);
        $location.replace().path($location.path() + '/' + type);
    }

@artch artch closed this as completed in f3202a9 Apr 7, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants