I am trying to watch a variable within an array that is bound to a repeating element using v-repeat but it doesnt seem to be working. There is a fiddle here
My HTML looks something like this
<div id="test">
<div v-repeat="details">
<select v-model="hour">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</div>
<pre>
{{ $data | json }}
</pre>
</div>
and the vue js like this
new Vue({
el: '#test',
data: {
details: [
{
hour: 2,
changeThis: null
},
{
hour: 3,
changeThis: null
}
],
},
watch: {
'details': function (val, oldVal) {
alert()
},
}
})
The binding to the hour properties is working fine, but I cannot get the watch to fire. I have also tried deep: true but that doesnt work either. If I define my watch as
watch: {
'details[0].hour'
then I can get it to work, but as I dont know how many details I will have then I dont really want to do this for each one. What I ideally want to do is get a reference to the array item that is being updated and then go from there. I could possibly achieve what I want by adding a
v-on="change:someVar(detail)"
to the select element and doing it that way but I'm curious as to why the approach I am trying does not work.
Edit It is actually possible to return the full array by using the deep watcher as @kishanterry notes below (thank you for that), I had overlooked this syntax in the docs.. However it still doesn't quite give the solution I was hoping for
// deep watcher
'c': {
handler: function (val, oldVal) { /* ... */ },
deep: true
}
true
, the whole watched array is passed as val. Your best bet is to usev-on="change: ..."
event listening format – kishanterry Aug 11 '15 at 9:11