ui-router 嵌套路由控制器未被调用 [英] ui-router nested route controller isn't being called
问题描述
我正在尝试调用应该与 home.category
路由链接的控制器,但它没有被调用.有什么问题?
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);
}
})
推荐答案
嗯,我从 ui-router
的给定文档中找到了线索
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一个>
但是我尝试添加template
还是不行,然后我看到我的父路由模板没有<div ui-view></div>
(我错误地删除了它)所以当我将它添加回来时它起作用了:),所以,要实例化我们孩子的路由控制器,我们必须有 <div ui-view></div>
在我们父级的路由模板中.
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.
这篇关于ui-router 嵌套路由控制器未被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!