Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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
    }
share|improve this question
    
I tried your fiddle with options in the docs: link but seems like the watcher returns a copy of the variables its watching (not the reference) and even if deep option is set to true, the whole watched array is passed as val. Your best bet is to use v-on="change: ..." event listening format – kishanterry Aug 11 '15 at 9:11
    
@kishanterry Thank you for taking a look. I had actually overlooked the syntax for the deepwatcher which may give me some of what I need. But as you say it may be best to fall back to v-on – Cookstaar Aug 11 '15 at 15:04
    
what exactly do you want in the watcher callback? – Evan You Aug 11 '15 at 15:26
    
@EvanYou it maybe that the v-on="change:someVar(detail)" is a better route but I was hoping to somehow get hold of the item in the array that has changed and then change some values in that. For example if 'hour' changes, then I want to do something to the adjacent 'changeThis' property – Cookstaar Aug 11 '15 at 15:40
3  
Sounds like you'd be better off making the repeated item a component! – Evan You Aug 11 '15 at 21:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.