1

I have an angular bootstrap tabset.

NOTE: this is a ui-bootstrap directive. So ng-class does not work.

To be able to change the class conditionally, I found here on SO:

  <tab heading="My Tab" class="{{classifyForm.$dirty ? 'tab-dirty':''}}">

Works great, when the form classifyForm is dirty, it adds the class tab-dirty and otherwise removes it.

Except now I need to do the same thing with another condition and another class in addition to what I have.

What is the way to combine the two expressions, so I get essentially:

if form is dirty,tab-dirty (what I have now) and if foobar then tab-foobar

so, if the form is dirty and foobar is true, the resulting tab should have both tab-dirty and tab-foobar classes, if only one is true then only have that one class.

2
  • I would create a method in the controller to generate the classes string, perhaps with arguments :) Commented Jun 9, 2016 at 18:43
  • That's a good approach. There might not be any other way. Commented Jun 9, 2016 at 18:45

2 Answers 2

1

It is very annoying that you can't use ng-class on this but you can do the following

<tab heading="My Tab" class="{{ (classifyForm.$dirty ? 'tab-dirty ':'') + (classifyForm.foobar ? 'tab-foobar ':'')}}">
Sign up to request clarification or add additional context in comments.

3 Comments

I wasn't sure how to structure something like that. Thanks.
It works. I use the same kind of thing in my application.
I had to make n edit to ensure the added classes all have a space between them. Otherwise, in the above case you'd have gotten class="tab-dirtyfoobar"
1

Update: ng-class won't work on the angular ui tab directive like the OP mentions. Here is the link to one of logged bugs: https://github.com/angular-ui/bootstrap/issues/1741

They don't offer any good solutions to get around it. So the only way I see is making a function in the controller to handle the logic to apply the class(es).

For all other cases (where possible), you should use ng-class

<tab heading="My Tab" ng-class="{ 'tab-dirty': classifyForm.$dirty, 'your-other-class': someOtherProperty }">

3 Comments

I cannot use ng-class, this is a directive fro ui-bootstrap. If I could use ng-class, it would be as easy as you suggest.
Can you make a plunker for it? That's strange why ng-class wouldn't work.
"Tab" is a directive and the directive actually writes out its own bootstrap html code and transcludes its contents. Whoever created it did not allow for ng-class to be added. This is an older version of the directive, 0.13.1. Perhaps the current one is smarter, but this project is stuck in the old one.

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.