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

How to make angular ui-router's parent state always execute controller code when state changes? #840

Closed
egervari opened this issue Jan 29, 2014 · 3 comments

Comments

@egervari
Copy link

Let's say we have a parent-child relationship defined in our $stateProvider as follows:

.state('myProfile', {
    url: "/my/profile",
    templateUrl: 'my/profile.html',
    controller: 'MyProfileController'
}).state('myProfile.edit', {
    url: "/edit",
    templateUrl: 'my/profile.edit.html',
    controller: 'EditMyProfileController'
});

The idea here is that the parent myProfile state is non-editable, but the child myProfile.edit state is the actual form to edit the profile. Let's ignore if this is how a profile page should work - I'm just playing around with things and learning.

When a user submits the form, I use the $state object to go back to the parent page:

$scope.save = function() {
    userResource.update($scope.user, function() {
        SessionService.refresh();

        $state.go('myProfile'); // also tried with reload: true as well
    });
}

After the user saves the profile data - and it is getting saved properly - the parent's view does not get updated unless I hit F5 and refresh the browser.

One thing I have noticed is that if I make the child myProfile.edit state into a parent itself and not a child of the myProfile state, this problem actually goes away and things behave as expected (albeit, the layout looks bad like this).

It seems that UI Router is maybe caching the result of the parent, unsuspecting that things have changed in the model and that it needs to rerun the controller?

How can I keep my parent-child relationship while still having the parent myProfile page always execute its controller to reload its data? Basically, I want this to happen whenever $state is used or when a link is clicked that directs to myProfile. How can I do that?

And if I cannot do what I ask, then I am aware that I can setup a bunch of child states and have them work as intended... however, how can I set the default child state for the parent? For example, let's say I want myProfile.edit to be the default child state of myProfile - how can I do that? For example, if the user goes to /my/profile, it automatically loads the form in the child ui-view automatically, running both controllers defined in states.js. Is this possible?

Thanks!

@timkindberg
Copy link
Contributor

Ok several things:

  • Technically this is a dupe of Controller not reloaded when using back button #122. Parent's do not reload by design. You should be able to wire the parent to the child somehow, via a service or something to handle the syncing of data.
  • This border on a general help question, please post general help questions to stack overflow and reserve github for bug and feature requests.

Thanks!

@timkindberg
Copy link
Contributor

oh lastly we have a reload option that you can set to true on the options object when you call $state.go(), but unfortunately it currently has a bug where the controller is not reloaded. That will be your ticket though once its fixed.

@timkindberg
Copy link
Contributor

Watch #582

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

2 participants