[英]How to make parent li active when the child changes
I'm new to both ui router and angularjs. 我是ui路由器和angularjs的新手。 The problem I'm facing is -
我面临的问题是-
In my header 在我的标题中
<li ng-class="{active: $state.includes('settings')}" id="header01">
<a ui-sref="settings.personal"> <span id="header02" > Settings</span> </a>
</li>
In my main.js i have - 在我的main.js中,我有-
$stateProvider.state("settings",{
abstract: true,
url: '/Settings',
templateUrl: 'Settings/settings.html',
resolve: {
},
controller: ['$scope', '$state', 'contacts', 'utils',
]
});
$stateProvider.state("settings.personal",{
url:'/Personal',
controller: "settingPersonal",
templateUrl: "Settings/personal.html"
});
$stateProvider.state("settings.additional",{
url:'/Additional',
controller: "settingPersonal",
templateUrl: "Settings/Additional.html"
});
$stateProvider.state("settings.reset",{
url:'/ResetPass',
controller: "myCtrl2 as second",
templateUrl: "Settings/password_reset.html"
});
Now when i change from personal to any other tab, the settings in header becomes inactive. 现在,当我从“个人”更改为任何其他选项卡时,标题中的设置将变为无效。 How to solve this.
如何解决这个问题。
Thank you. 谢谢。
One approach would be to use state.data
, eg: 一种方法是使用
state.data
,例如:
.state('settings', {
url: '/settings',
templateUrl: 'settings/settings.html',
data: {
pageTitle: 'Settings'
}
})
Then in your view you can access $state.current.data
: 然后,您可以访问
$state.current.data
:
<li ng-class="{active: $state.current.data.pageTitle('Settings')}">
<a ui-sref="settings">Settings</a>
</li>
<li ng-class="{active: $state.current.data.pageTitle('About')}">
<a ui-sref="settings">About</a>
</li>
But personally, i'd probably put the nav items in an ng-repeat, and create the page title/class dynamically with state.data
, to avoid hard coding page titles. 但就我个人而言,我可能会将导航项放在ng-repeat中,并使用
state.data
动态创建页面标题/类,以避免对页面标题进行硬编码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.