2

Is there a way to bind input to objects instead of single variables.

I know we can do this simple trick

<input v-model="name">

But the following doesn't work:

<input v-model="user.name">

That's what i was used to in Angular, is there a way to achieve this in vue.js?

2 Answers 2

3

you can bind directly to data, code as follow:

var demo = new Vue({
    el: "#demo",
    data: {
      user: {
        name: "please enter"
      }
    }
})
<script src="https://unpkg.com/vue/dist/vue.min.js"></script>
<div id="demo">
  <input v-model="user.name">
  <span>{{user.name}}</span>
</div>

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

Comments

1

This works in Vue as well, make sure to define complete object in data which will make it reactive, Here is a working fiddle.

Vue Code:

var demo = new Vue({
    el: '#demo',
    data: function(){
        return {
        user: {
            name: 'This is working fine'
        }
      };
    }
})

Comments

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.