3

I have the following nested routes in my Angular app:

.state('mapping', {
    url: '/mapping',
    templateUrl: 'app/components/mapping/mapping.html',
    controller: 'MapCtrl as map',
    abstract: true,
    authenticate: true
})
.state('mapping.all', {
    url: '',
    templateUrl: 'app/components/mapping/partials/all.html',
    authenticate: true
})
.state('mapping.project', {
    url: '/:projectName',
    controller: 'ProjectCtrl as proj',
    templateUrl: 'app/components/mapping/partials/project.html',
    authenticate: true
})

When accessing 'mapping.project', ProjectCtrl never gets loaded. Instead, MapCtrl is loaded as if I was still on the parent state.

How can I override the parent controller with another controller that is specific to that child state?

I want this controller to get loaded every time I access that specific child state, which just won't happen if I have a single parent controller for every child state.

2
  • Can u share the final url? Does it contain a projectName? Commented Oct 11, 2015 at 8:33
  • Yes, it does contain a project name. Commented Oct 11, 2015 at 8:35

1 Answer 1

3

Any controller is related to view (not to state). It means, that if:

...When accessing mapping.project, ProjectCtrl never gets loaded....

That view was not loaded. And it mostly means, that parent view (app/components/mapping/mapping.html) did not contain target:

<div ui-view=""></div>

So, if we will place into parent template a target (ui-view=""), child state will inject its view into that place. But that will not replace the parent controller. It will

  • call parent ctrl when parent (or child directly) is accessed
  • if child is accessed from parent, parent ctrl won't be reinit, but child ctrl will be initiated

Check these for more details:

Sign up to request clarification or add additional context in comments.

7 Comments

I do have a <div ui-view></div> within mapping.html. Should I replace it with <div ui-view=""></div>?
No, that won't be the issue ... I could show you that in a plunker.. sec please
I've seen this plunkr you created: plnkr.co/edit/qzI25MKsXBUvw2b45bef?p=preview This problem is very very similar to what I have. I also need a lot of nested views with a sidebar on my Angular application.
Exactly... that is the way to go
Here is the plunker plnkr.co/edit/1aQbOos1aoGhUIAg30r6?p=preview - it does what described above. If we go to mappings - substate all is selected as the default. If we pass the id, every time the ProjectCtrl is called, because its view is injected into parent ui-view (I added alert to that ctrl to show that feature... could be turned off ;)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.