2

I have a scenario where I want the v-model binding of an Input field to be decided by the value returned by a computed property.

Please see the example below:

<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/[email protected]/dist/vue.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />  
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <div id="app" class="container-fluid">
    <h2>Input Binding</h2>
    <p><b>First Name</b></p>
    <input type="text" v-model="value.first" placeholder="Enter first name" />
    <p style="margin-top:20px;"><b>Second Name</b></p>
    <input type="text" :v-model="lastName" placeholder="Enter last name" />
    <hr />
    <p>{{value.first}} {{value.second}}</p>
    <hr />
    <p>Value of computed property: {{lastName}}</p>
  </div>
  <script>  
   new Vue({
      el: '#app',
      data: function() {
        return {
          value: {
          first:'',
          second:''}
        }
      },
    computed: {
        // a computed getter
        lastName: function() {
            return 'value.second'
        } 
      }     
    });
  </script>
</body>
</html>

As you can see in the above code, I want the first field to be bound to value.first so I have chosen v-model="value.first". For the second field, I want the model binding to be decided by the value returned from computed property. Right now it's a simple example and there's only one returned value, i.e., value.second. But this will be decided on logic.

Right now when I try to do that binding, it doesn't return anything. Any help is greatly appreciated.

1 Answer 1

11

You can make use of a computed setter as follows:

computed: {
    lastName: {
        get(){
            //perform your logic
            return 'value.second'
        },
        set(newValue){
            this.value.second = newValue;
        }

    } 
  }     

Now use this computed property as v-model in your template;

<input type="text" v-model="lastName" placeholder="Enter last name" />

Here is the fiddle

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

5 Comments

Oh, I guess this is what he meant. I had trouble understanding it
Hi Vamsi, this is what I wanted. I think it's doing it's job. But there is a small trouble. The second input field continues to show 'value.second' filled in. If I quicly type something, it does bind to value.second data property correctly. But then it gets replaced by value.second text everywhere. How do I correct this?
@asanas can you reproduce a fiddle with the logic also?
@VamsiKrishna Thanks so much. It works perfectly. I was out for some time hence couldn't reproduce the fiddle. Sorry about that.
@VamsiKrishna You solution worked great form me. But now I have a bit more complication in it and I am again stuck. I have posted a new question on similar line. If you can help... stackoverflow.com/questions/47805993/…

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.