Currently in the process of learning angularJs
, I am still new to it and attempting to dynamically add an active class
to the recently clicked tab. Within my interface, there are four tabs:
insert, view, update & delete
. My goal is to apply the active class to the 'insert' tab when it's clicked, then switch it to the 'view' tab upon clicking that, and so forth. Despite my attempts, I have been unsuccessful in achieving this functionality. My current code automatically adds the active
class to all tabs simultaneously, instead of individually as desired. Any advice or suggestions on how to rectify this issue would be greatly appreciated. Thank you for your patience with my linguistic shortcomings!
body{
background-color: #e8e8e8;
margin: 0;
padding: 0;
}
.menu-content{
padding: 30px;
width: 100%;
margin: 0 auto;
text-align: center;
}
.menu-content button{
background-color: dimgrey;
color: #FFF;
font-family: "Helvetica Neue", Arial, "Lucida Grande", sans-serif;
font-size: 16px;
border: 1px solid dimgrey;
height: 35px;
width: 100px;
}
.menu-content button:hover{
color: gold;
cursor: pointer;
}
.crud{
padding: 30px;
width: 90%;
margin: 0 auto;
text-align: center;
background-color: #fff;
border: 1px solid #aaa;
}
.active{
color: gold !important;
cursor: pointer !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div class="container" ng-app="myApp" ng-controller="myController">
<div class="menu-content">
<button ng-click="activeClass('insert')" ng-class="{active: true}">Insert</button>
<button ng-click="activeClass('view')" ng-class="{active: true}">View</button>
<button ng-click="activeClass('update')" ng-class="{active: true}">Update</button>
<button ng-click="activeClass('delete')" ng-class="{active: true}">Delete</button>
</div>
<div class="crud" ng-show="menu.insert">Insert</div>
<div class="crud" ng-show="menu.view">View</div>
<div class="crud" ng-show="menu.update">Update</div>
<div class="crud" ng-show="menu.delete">Delete</div>
</div>
<script>
(function(){
var app = angular.module('myApp',[ ]);
app.controller('myController', function($scope){
$scope.menu = {};
$scope.menu.insert = true;
$scope.menu.view = false;
$scope.menu.update = false;
$scope.menu.delete = false;
$scope.activeClass = function(tab){
$scope.menu.insert = false;
$scope.menu.view = false;
$scope.menu.update = false;
$scope.menu.delete = false;
if(tab == 'insert') $scope.menu.insert = true;
else if(tab == 'view') $scope.menu.view = true;
else if(tab == 'update') $scope.menu.update = true;
else $scope.menu.delete = true;
}
});
})();
</script>