DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Frameworks
  4. Bootstrap Tabs with Angular.js

Bootstrap Tabs with Angular.js

Christian Grobmeier user avatar by
Christian Grobmeier
·
Dec. 23, 12 · Interview
Like (0)
Save
Tweet
Share
25.19K Views

Join the DZone community and get the full member experience.

Join For Free

Twitter Bootstrap Tabs are a very popular feature – at least for me. I have them several times in my app. As part of my migration to Angular.js I want to use directives to switch between tabs. This has turned out so much easier than expected.

Assume this HTML code:

<ul class="nav nav-tabs">
    <li class="active"><a href="#standardNav">Standard</a></li>
    <li><a href="#quickadd">Quick Add</a></li>
</ul>
<div class="calendarNav tab-pane fade in active" id="standardNav">
    Pane 1
</div>
<div id="quickadd" class="tab-pane fade">
    Pane 2
</div>

This is some kind of basic Bootstrap Tab. In pure jQuery land you would do something like that to active the tabs:

$('.nav-tabs a').click(function (e) {
    e.preventDefault();
    $(this).tab('show');
});

But with Angular.js it is recommended to write directives for all UI manipulations. Therefore I added the showtab directive to my navigation:

<ul class="nav nav-tabs">
    <li class="active"><a showtab="" href="#standardNav">Standard</a></li>
    <li><a showtab="" href="#quickadd">Quick Add</a></li>
</ul>

Note: I could have left of equality and quote symbols, but some browser might complain. I plan some backwards compatibility and so…

And finally added a new directive to my App:

var directives = angular.module('directives');
 
directives.directive('showtab',
    function () {
        return {
            link: function (scope, element, attrs) {
                element.click(function(e) {
                    e.preventDefault();
                    $(element).tab('show');
                });
            }
        };
    });

The element you have annotated with this directive will be linked to the function above. Basically I just add an click event handler to it, doing the same thing as with jQuery. Just that I use “element”, which is the link used for navigation.

Note 2: don’t try to use camel case. For example, showTab would not match to showTab in the directive. Not sure why, but if you feel your directive is not called, check if everything is lower case.Olov Lassus explained to me how to deal with camel case in Angular. It’s quite easy! Use <a show-tab ...> to map to directives.directive("showTab", …). Thanks Olov (again)!



Bootstrap (front-end framework)

Published at DZone with permission of Christian Grobmeier, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • ChatGPT: The Unexpected API Test Automation Help
  • What Is Policy-as-Code? An Introduction to Open Policy Agent
  • Secrets Management
  • Express Hibernate Queries as Type-Safe Java Streams

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: