1

I'm trying to render the list with vue.js. The data has come from api call.

List rendering is ok, until I try to add data-feature_id="@{{searched_feature.id}}" in li tag. When I add it, I got an error:

[Vue warn]: Error compiling template:......

<div class="search_feature_select">
    <ul>
        <li v-for="searched_feature in searched_features" data-feature_id="@{{searched_feature.id}}" @click="add_to_features_list">
        @{{searched_feature.name}}
        </li>
    </ul>
</div>

How can I put searched_feature.id in data-feature_id attribute?

4 Answers 4

2

You need to add : before the attribute

<div class="search_feature_select">
    <ul>
        <li v-for="searched_feature in searched_features" :data-feature_id="searched_feature.id" @click="add_to_features_list">
        {{searched_feature.name}}
        </li>
    </ul>
</div>
Sign up to request clarification or add additional context in comments.

Comments

0

if you wish to add the '@' in front of the data-feature_id attribute:

<div class="search_feature_select">
    <ul>
        <li v-for="searched_feature in searched_features" :data-feature_id="'@' + searched_feature.id" @click="add_to_features_list">
        {{searched_feature.name}}
        </li>
    </ul>
</div>

Comments

0

https://v2.vuejs.org/v2/guide/list.html coughcough*

var app = new Vue({
  el: "#app",
  data: {
    searched_features: [
      {
        id: 1,
        name: "One"
      },
      {
        id: 2,
        name: "Two"
      },
      {
        id: 3,
        name: "Three"
      }      
    ]
  },
  methods: {
    add_to_features_list(){}
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <div class="search_feature_select">
    <ul>
        <li v-for="searched_feature in searched_features" :data-feature_id="searched_feature.id" @click="add_to_features_list">
        @{{ searched_feature.name }}
        </li>
    </ul>
  </div>
</div>

Comments

0

Add 'v-bind:' prefix of shorthand ':' before attribute to bind dynamic data.

<div class="search_feature_select">
  <ul>
    <li v-for="searched_feature in searched_features" 
    :data-feature_id="searched_feature.id" 
    @click="add_to_features_list">
     {{searched_feature.name}}
    </li>
  </ul>
</div>

See more https://v2.vuejs.org/v2/api/#v-bind

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.