0

Actually , this is my problem in my real project doing with API . Let me first explain that , I used axios for call api data . That will get json array and each array has the same radio value so I append radio value to each array . Although I want to get changed radio value by v-model , but it is not working . I outputted selected value under radio element . Below I demonstrated like my project code .

var app2 = new Vue({
  el: '#app-2',
  data: {
    list: null
  },
  created: function () {
    var json = [{id:1,name:"B"},{id:2,name:"D"}]
    this.list = json
    this.list.forEach(function (v) {
       v.options = [{ text: 'pacageA' , value: 1},{text: 'pagckaeB',value:2}]
       v.selected = null
    })
  }
})
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
<div id="app-2">
  <div v-for="l,ix in list">
     <div v-for="o in l.options">
      <input type="radio" v-model="l.selected" :name="'test'+ix" :value="o.value">
      </div>
          <h3>{{l.selected}}</h3>
  </div>
</div>

1 Answer 1

3

Your problem is a reactivity one. In order for Vue to know about the new object properties you're adding to your list, you should use Vue.set, eg

Vue.set(v, 'options', [{ text: 'pacageA' , value: 1},{text: 'pagckaeB',value:2}])
Vue.set(v, 'selected', null)

Or, as mentioned below, "do all the modifications to json before assigning it to this.list".

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

1 Comment

Alternatively, do all the modifications to json before assigning it to this.list.

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.