使用AngularJS取消激活Bootstrap Navbar按钮

时间:2016-05-09 01:50:57

标签: javascript css angularjs twitter-bootstrap navbar

这里我有一个引导程序导航栏的代码:



var cvApplication = angular.module('cvApplication', []);

cvApplication.controller('contentCtrl', function ($scope) {
    $scope.navButtons = ["Home", "About", "Contacts"];
    $scope.btnActive = "About";
});

<!DOCTYPE html>
<html lang="en" ng-app="cvApplication" ng-cloak ng-init="">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
        <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
        <link rel="stylesheet" href="style.css">
    </head>
    <body>
        <nav class="navbar navbar-default navbar-static-top" ng-controller="contentCtrl">
            <div class="container-fluid">

                <div class="navbar-header">
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#mainNavBarToggler">
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    <a href="#" class="navbar-brand">Subjects</a>
                </div>

                <div class="collapse navbar-collapse" id="mainNavBarToggler">
                    <ul class="nav navbar-nav">
                        <li ng-repeat="navButton in navButtons" ng-class="{ active: btnActive == navButton }" ng-click="btnActive = navButton">
                            <a href="#">{{navButton}}</a>
                        </li>
                    </ul>
                </div>
            </div>
        </nav>
      
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
        <script src="script.js"></script>
    </body> 
</html>
&#13;
&#13;
&#13;

一切都很好,除了当你点击导航中的一个按钮然后点击另一个按钮时,你点击的上一个按钮没有被取消激活(也就是&#39;有效&#39; ;课程未被删除,但仍然突出显示)。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

您可以使用控制器中的功能更新活动按钮的变量。

var cvApplication = angular.module('cvApplication', []);

cvApplication.controller('contentCtrl', function ($scope) {
    $scope.navButtons = ["Home", "About", "Contacts"];
    $scope.btnActive = "About";
    $scope.updateActive = function(navButton) {
            $scope.btnActive = navButton;
    };
});
<!DOCTYPE html>
<html lang="en" ng-app="cvApplication" ng-cloak ng-init="">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
        <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
        <link rel="stylesheet" href="style.css">
    </head>
    <body>
        <nav class="navbar navbar-default navbar-static-top" ng-controller="contentCtrl">
            <div class="container-fluid">

                <div class="navbar-header">
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#mainNavBarToggler">
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    <a href="#" class="navbar-brand">Subjects</a>
                </div>

                <div class="collapse navbar-collapse" id="mainNavBarToggler">
                    <ul class="nav navbar-nav">
                        <li ng-repeat="navButton in navButtons" ng-class="{ active: btnActive == navButton }" ng-click="updateActive(navButton)">
                            <a href="#">{{navButton}}</a>
                        </li>
                    </ul>
                </div>
            </div>
        </nav>
      
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
        <script src="script.js"></script>
    </body> 
</html>

Here是类似的解决方案。

我不知道为什么它适用于函数,但不适用于视图中的插值。可能,您可以查看许多本地范围。