2

I have a select element with options, which loops through an array of objects (services).

<select placeholder="Vælg ydelse">
    <option v-for="service in services" :key="service.id">
        {{service.name}}
    </option>
</select>

In my data object, I have

chosenService: {
    id: 42,
    name: "some service",
    group: {
        id: 2,
        name: "some group within the service",
    },
    additions: [],
    classSignupMoreActive: false,
},

Which I'd like to be retrieved once the select is changed.

Finally, I'm sending the services array from the parent, as all of this is happening in a component. I'm not sure if this is the most correct way to do it.

Like this:

<Modal
  :show="showModal"
  @close="showModal = false"
  title="Modal Title"
  :services="this.services"
>
</Modal>

1 Answer 1

4

You can use v-model and pass object to :value property of option:

new Vue({
  el: "#demo",
  data() {
    return {
      services: [{id: 42, name: "some service", group: {id: 2, name: "some group within the service",}, additions: [], classSignupMoreActive: false,}, {id: 43, name: "some other service", group: {id: 2, name: "some group within the service",}, additions: [], classSignupMoreActive: false,}],
      chosenService: {},
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="demo">
  <select v-model="chosenService" placeholder="Vælg ydelse">
    <option v-for="service in services" :key="service.id" :value="service">
        {{service.name}}
    </option>
  </select>
  chosenService = {{ chosenService }}
</div>

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

1 Comment

Thank you so much! I'd tried using v-model, but didn't use :value with it which didn't make the binding probably.

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.