InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

AngularJS

  • Hello world
  • ng-init
  • ng-model
  • Controller to increment value
  • Controller - basic clock
  • Controller - ajax
  • date filter
  • include template (url)
  • include template (inline)
  • tabs using ng-switch
  • conditional style using ng-style
  • animation with ng-class
  • ng-if vs ng-hide/ng-show

AngularJS Directives

  • ng-bind
  • ng-bind-html
  • ng-bind-template
  • ng-blur
  • ng-change
  • ng-checked
  • ng-class
  • ng-class-even and ng-class-odd
  • ng-click
  • ng-cloak
  • ng-dblclick
  • ng-disabled
  • ng-focus
  • ng-hide
  • ng-href
  • ng-if
  • ng-list
 
  • Home
  • > Tutorials
  • > Javascript
  • > AngularJS

AngularJS – implement tabs using ng-switch

on Mar 14, 2016

Angularjs ng-switch directive is used to conditionally swap DOM structure on your template based on a scope expression.

ng-switch usage

<ANY ng-switch="expression">
  <ANY ng-switch-when="matchValue1">...</ANY>
  <ANY ng-switch-when="matchValue2">...</ANY>
  <ANY ng-switch-default>...</ANY>
</ANY>

Example – horizontal tabs using ng-switch

<style>
.tabmenu {margin-top: 20px; border-bottom: 1px solid;}
.tabmenu li.active {border: 1px solid; border-bottom: 1px solid #fff;}
.tabmenu li {display:inline; cursor: pointer; padding: 0 10px;}
</style>

<div ng-app="myApp" ng-controller='MyCtrl'>
  <ul class="tabmenu">
    <li ng-class="{'active': tabName=='tab1'}" ng-click="tabName='tab1'">Tab1</li>
    <li ng-class="{'active': tabName=='tab2'}" ng-click="tabName='tab2'">Tab2</li>
  </ul>

  <div ng-switch="tabName">
    <div ng-switch-when="tab1"><p>tab1 content</p></div>
    <div ng-switch-when="tab2"><p>tab2 content</p></div>
  </div>
</div>

<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular.min.js"></script>
<script>
var module = angular.module('myApp', []);
module.controller('MyCtrl', function($scope) {
  $scope.tabName = "tab1";
});
</script>
refresh done
try it online

Few points to note:

  1. We can use ng-include to include a template in ng-switch-when content section.
  2. Here ng-class is being used to have a class name if a specific condition is met.
    ng-class="{'active': tabName=='tab1'}"
    

    It can also be used multiple times in same element.

Suggested posts:

  1. How to use rsync for production release automation
  2. HTML input checkbox
  3. How to delete orphan wordpress wp_postmeta rows
  4. CSS general sibling selector
  5. Make an element draggable using Vanilla Javascript
  6. AngularJS ng-class-even and ng-class-odd example
  7. Javascript prototype examples
  8. AngularJS ng-dblclick examples
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged AngularJS, Javascript, Tutorials
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress