AngularJS sets the initial active class

I use Bootstrap tabs with Angular ng- repeat .

The problem I am facing is that the first li and the first div.tab-pane require an active class. Bootstrap will take care of this after clicking the tab, but as you can see, not when loading.

My question is, how can I set the active class for the first li and div.tab-pane ?

Demo:

http://jsfiddle.net/FQgHx/

+6
source share
1 answer

All you have to do is put the ng-class directive on the tab and tab bar that you would like to activate initially. You can change the expression from $index == 0 in the ng-class directive to whatever you want. This may use a function or variable in your scope.

Here is a JS script showing how to do this: http://jsfiddle.net/digitalzebra/qqPZd/

And here is the final code ... pay attention to the ng-class directives:

 <div ng-app> <div ng-init="names = [{name:'one'}, {name:'two'}, {name:'three'}, {name:'four'}, {name:'five'}]"> <ul class="nav nav-tabs"> <li ng-repeat="name in names" ng-class="{active: $index == 0}"> <a href="#tab{{$index + 1}}" data-toggle="tab">Week {{acute.Week}}</a> </li> </ul> <div class="tab-content"> <div class="tab-pane fade in" id="tab{{$index + 1}}" ng-repeat="name in names" ng-class="{active: $index == 0}"> <p>{{$index + 1}}</p> </div> </div> </div> </div> 
+14
source

Source: https://habr.com/ru/post/946187/


All Articles