Skip to content

Instantly share code, notes, and snippets.

@tondol
Last active August 29, 2015 14:10
Show Gist options
  • Save tondol/ec9bec495fd1b6fabc63 to your computer and use it in GitHub Desktop.
Save tondol/ec9bec495fd1b6fabc63 to your computer and use it in GitHub Desktop.
Vue.js: viewから配列の要素を参照する
<script src="http://cdnjs.cloudflare.com/ajax/libs/vue/0.11.0/vue.min.js"></script>
<div id="container">
<div>
0:{{array[0].name}}, {{array[0].prop}}
</div>
<div v-repeat="array" v-show="$index > 0">
{{$index}}:{{name}}, {{prop}}
</div>
</div>
<script>
var app = new Vue({
el: "#container",
ready: function() {
var self = this
setTimeout(function() {
// it works!
var f = function(i) {
var o = self.array[i]
o.$add("prop", true) // we must use the $add method to add a property
self.array.$set(i, o) // we must use the $set method to modify an element of array
}
f(0)
f(1)
f(2)
// it doesn't work!
// self.array[0].$add("prop", true)
// self.array[1].$add("prop", true)
// self.array[2].$add("prop", true)
}, 1000)
},
data: {
array: [
{ name: "a" },
{ name: "b" },
{ name: "c" }
]
}
})
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment