3

I need to pass a dynamic variable into v-if attribute. I tried multiple ways but it doesn't produce the expected result.

So far, I have this: v-if="customDropdown === {{row.name}}"

How can I conditionally and dynamically render the element, please? Thank you in advance.

3
  • Don't use interpolation... v-if="customDropdown == row.name" Commented Nov 28, 2018 at 7:09
  • @DawidZbiński , It helped! Thanks :) Commented Nov 28, 2018 at 7:23
  • Added it as an answer, would appreciate marking it as correct. Commented Nov 28, 2018 at 7:51

4 Answers 4

2

You cannot use interpolation in Vue directives/attributes.

To bind to v-if or v-for use variables directly:

<div v-if="value.someProperty"></div>

To bind to other attributes/properties use v-bind: or shorthand : as follows:

<div :directive="value"></div>

Template Syntax documentation

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

Comments

1

You need binding the row.name to data object in vue:

export default { data() { return { customDropdown: 'something', row: {name: 'something'}, } } }

and then use it in the v-if statement:

<div v-if="customDropdown == row.name">You see me if customDropdown is equal to row.name</div>

Comments

1

First, you cannot interpolate directives/attributes and second, it's not clear, but I think you want to check the condition while iterating by a array.

You can use the template structure below:

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <div
    v-for="row in arrayList"
    v-if="customDropdown === row.name"
    >
    Show row content: {{ row }}
  </div>
</div>

With the data structure below to achieve you goal:

new Vue({
  el: '#app',
  data: {
    customDropdown: 'first',
    arrayList: [
      {
        name: 'first',
      },
      {
        name: 'second',
      },
     ],
  },
})

Comments

0

Just use the properties name,

For example

export default {
 data() {
    return {
       var: true 
     }
  }
}

html

<span v-if="var">I only appear when the condition is true</span>

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.