2

I am using vuejs and I would like to switch my class depending on the data value. If its a negative number, i would like to use the .neg class and when its a positive number, i would like to use the .pos class.

Everything is working, except for adding the class.

The data looks like this: 5, -7, 8, -2, 4, -9 ect

myArray: function () {
  var test =  [5, -7, 8, -2, 4, -9];
  return test;
},

<div v-for="data in myArray">
  <div v-bind:class="{'neg': data < 0, 'pos': data > 0}"></div>
  <div id="my">{{ data }}</div>
</div>

.pos {background-color: green;}
.neg {background-color: red;}

Any help would be appropriated.

4
  • Shouldn't it be data in myArray()? Also, I wouldn't keep re-using the "my" id in the loop. You'll end up with duplicates Commented Nov 1, 2017 at 0:04
  • Seems to work ok here ~ jsfiddle.net/o49x5de0/2. Could it possibly be that your neg / pos classed elements have no content? Commented Nov 1, 2017 at 0:10
  • Voting to close as a typo Commented Nov 1, 2017 at 0:12
  • Thanks Phil, would give you an up vote if i could. My issue was myArray(). When i changed it, it worked Commented Nov 1, 2017 at 12:26

1 Answer 1

3

Since, myArray is returning a value (an array). Replace myArray with myArray()

<div v-for="data in myArray">
  <div :class="{'neg': data < 0, 'pos': data > 0}"></div>
  <div id="my">{{ data }}</div>
</div
Sign up to request clarification or add additional context in comments.

Comments

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.