0

I have the following element:

<input type="text" v-model="selectedPost.title" v-bind:value="selectedPost.title">

and I have the following Vue.js app:

var vueapp = new Vue({
  el: '#app',
  data: { 
          selectedPost: {} 
        }
});

When I type something on the input, the Vue model is updated, I can see it unning this on my browser console:

vueapp.$data.selectedPost.title

Returns the value typed in the textbox. All good.

But.. when I do this:

vueapp.$data.selectedPost.title = "changed";

The textbox does not update with the assigned value.

Why? How to make it work?

This jsfiddle shows the issue happening: https://jsfiddle.net/raphadko/3fLvfea2/

3
  • Could you create a complete script example so it is easier for us to debug? Commented Aug 9, 2017 at 19:28
  • Not able to reproduce this issue: jsfiddle.net/oa0o3rf2 Commented Aug 9, 2017 at 19:38
  • updated with a jsfiddle Commented Aug 9, 2017 at 19:41

1 Answer 1

2

The behavior you are seeing is because Vue cannot detect properties added to objects after the Vue has been initialized unless you add them using $set. In this case, just initialize the title property.

var vueapp = new Vue({
  el: '#app',
  data: { 
          selectedPost: {title:''} 
        }
});

Updated fiddle.

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

2 Comments

Would that be the same for sub-sub-objects? What if title is another object? I would have to initialize all it's properties beforehand?
@raphadko Yes, any object you add a property to. You don't have to initialize them, but if you don't you need to use the $set method I linked.

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.