Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
946 views
in Technique[技术] by (71.8m points)

angularjs - ui-router nested route controller isn't being called

I am trying to call a controller which should be linked with the home.category route but it isn't being called. What's wrong in it?

$stateProvider    
  .state("home", {
    // Use a url of "/" to set a states as the "index".
    url: "/",
    templateUrl: APP_CONFIG.baseUrl +
      'partials/layouts/home.html',
    controller: 'MainCtrl'
  })
  .state("home.category", {
    // Use a url of "/" to set a states as the "index".
    url: "c/:categoryId/:categorySlug",
    controller: function($stateParams) {
      alert($stateParams.categoryId);
    }
  })
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Well, I found a clue from the given documentation of ui-router the says

You can assign a controller to your template. Warning: The controller will not be instantiated if template is not defined.

https://github.com/angular-ui/ui-router/wiki#controllers

But I tried to add template and still didn't work, then I saw that my parent route template didn't have <div ui-view></div> (I mistakenly removed it) so when I added it back it worked :), So, to instantiate our child's route controller, we must have <div ui-view></div> in our parent's route template.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...