4

I'm trying to create simple tabs using only divs with ng-show and ng-hide. I don't want to use jQuery or BootStrap. I have created the tabs but I cannot handle the divs correctly.

JS Fiddle

<div  ng-app ng-controller="sample">

    <div class="cb" ng-click="showDetails = ! showDetails">tab 1</div>
    <div class="cb" ng-click="showDetails = ! showDetails">tab 2</div>

    <div ng-hide="showDetails">
        <p>first</p>
     </div>

    <div ng-show="showDetails">
        <p>second</p>
    </div>

Even if I click the first tab displays the content of the second tab. How can I display only the first content when I click the first tab and the second content when I click the second one?

Thank you in advance.

2
  • @F4r-20, are there any description about markdown like you used for JSFiddle? Commented Jun 24, 2013 at 12:41
  • @SET use the <kbd> tag. Commented Dec 3, 2013 at 19:11

1 Answer 1

19

http://jsfiddle.net/x8xfM/2/

<div  ng-app ng-controller="sample" ng-init="tab=1">

        <div class="cb" ng-click="tab = 1">tab 1</div>
        <div class="cb" ng-click="tab = 2">tab 2</div>

        <div ng-show="tab == 1">
            <p>first</p>
         </div>

        <div ng-show="tab == 2">
            <p>second</p>
        </div>
</div>

In your case showDetails = !showDetails will switch active tab each time you click on ANY tab.

Sign up to request clarification or add additional context in comments.

1 Comment

Great answer! Works nicely.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.