2

Here is an example of what I want to achieve

data-ng-class="{ 'tooltip_show' : showTooltip , 'tooltip__' + brand.settings.name }" 

but it doesn't work.

6
  • 1
    Why there are two values inside one ng-class for 'tooltip_show' ? Commented Jul 14, 2017 at 13:18
  • I need have one class which depends on showTooltip, and other which depends on this variable brand.settings.name. Can i do this in another way? Commented Jul 14, 2017 at 14:00
  • As @harriyot mentioned . this should suffice { 'tooltip_show' : showTooltip , 'tooltip__' + brand.settings.name : tooltipText } Commented Jul 14, 2017 at 14:01
  • This doesn't work. Commented Jul 14, 2017 at 14:09
  • What is happening? Do you get any errors? Can you post more code on how you are trying to achieve this? Commented Jul 14, 2017 at 14:10

3 Answers 3

3

Use the array form for ng-class:

<div ng-class="[showTooltip ? 'tooltip_show' : '', 'tooltip__' + brand.settings.name]"> <div> 

OR compute the class in JavaScript:

<div ng-class="computeClass(tooltip_show, brand.setting.name)"> </div> 
$scope.computeClass(show, name) { var obj = {}; obj.showTooltip = show; obj['tooltip_'+name] = true; return obj; }; 

The later approach is more easily debugged and better for complex computation.

See also,

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

1 Comment

Yeah this is what I think you can do @стас Пилипко, nice answer georgeawg :+1:
0

It looks like you haven't set a value for the second item. Did you mean something like

{ 'tooltip_show' : showTooltip , 'tooltip__' + brand.settings.name : tooltipText } 

or

{ 'tooltip_show' : showTooltip , 'tooltip__' : brand.settings.name } 

?

1 Comment

'tooltip__' + brand.settings.name - when i write only this part, i receive somerthing like this class="tooltip__class". brand.settings.name - it is variable and depends on the page.
0

http://jsbin.com/genaqapefe/edit?html,js,output

data-ng-class="{ 'tooltip_show': showToolTip, {{ 'tooltip_' + brand.settings.name }}: true }" 

This is working for me in this bin. I couldn't get it to evaluate without the curly braces, although not sure if that's the best practice.

4 Comments

@ стас Пилипко or did you want it to always show that? You could just use a class, like: class="{{ 'tooltip_' + brand.settings.name }}" data-ng-class="{ 'tooltip_show': showToolTip }"
You should not use interpolation in the value of the class attribute, when using the ngClass directive on the same element. See AngularJS ng-class Directive Reference - Known Issues.
@georgeawg yeah, you're right. I think a better answer to this question would probably be to restructure how the property is built to begin with. As in, not in the HTML but maybe in the Javascript.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.