2

I'm trying to get a child state working properly.

Each of these states shows up fine, when I test the corresponding urls. But there's one problem...

app.config(['$stateProvider', function($stateProvider) {
  $stateProvider.state('root', { // works 100%
    url: '/',
    controller: function($scope) {
      alert("state: root");
    },
    templateUrl: 'root.html'
  })
  .state('program', {  // works 100%
    url: '/program',
    controller: function($scope) {
      alert("state: program");
    },
    templateUrl: 'program.html'
  })
  .state('program.new', {  // works only sorta!
    url: '/new',
    controller: function($scope) {
      alert("state: program.new");
    },
    templateUrl: 'program.new.html'
  });
}]);

...For program.new, the correct state is being chosen (I'm logging transitions in the debugger using this: https://stackoverflow.com/a/20786262/2308190 ), but only the alert("state: program") fires, not the alert("state: program.new"). Plus, the program.html template shows up, instead of the program.new.html template.

Doesn't the controller of a child state get a chance to run? And why doesn't the child's template show up?

To be clear, Angular is figuring out the proper state, program.new. It just doesn't seem to run the controller or render the template.

1 Answer 1

1

Figured it out: the program.html template had no directive inside it, so there was nowhere for the child state, program.new, to render into... and the controller only runs if the view is going to render.

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

1 Comment

In fact, it looks like there are more situations in which the controller section is ignored... if you use ui-router named views, then the whole state's controller section is ignored, but you can put a controller section inside each of the named views!

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.