与AngularJS设置活动标签的风格 [英] Set active tab style with AngularJS

查看:123
本文介绍了与AngularJS设置活动标签的风格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在AngularJS设置这样的路线:

I have routes set in AngularJS like this:

$routeProvider
    .when('/dashboard', {templateUrl:'partials/dashboard', controller:widgetsController})
    .when('/lab', {templateUrl:'partials/lab', controller:widgetsController})

我对风格为标签上的顶栏的一些链接。我如何可以根据当前模板或URL添加主动类标签?

I have some links on the topbar styled as tabs. How can I add 'active' class to a tab depending on current template or url?

推荐答案

要解决这个问题,而不必依赖于网址的方法是在添加自定义属性,各部分的 $ routeProvider 配置,如:

A way to solve this without having to rely on URLs is to add a custom attribute to every partial during $routeProvider configuration, like this:

$routeProvider.
    when('/dashboard', {
        templateUrl: 'partials/dashboard.html',
        controller: widgetsController,
        activetab: 'dashboard'
    }).
    when('/lab', {
        templateUrl: 'partials/lab.html',
        controller: widgetsController,
        activetab: 'lab'
    });

在暴露你的控制器 $路线

function widgetsController($scope, $route) {
    $scope.$route = $route;
}

根据当前的活动选项卡上的活动类:

<li ng-class="{active: $route.current.activetab == 'dashboard'}"></li>
<li ng-class="{active: $route.current.activetab == 'lab'}"></li>

这篇关于与AngularJS设置活动标签的风格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆