[Vuejs]-How to add text in input form with v-model data bindings

2๐Ÿ‘

โœ…

You have to use the value attribute instead of v-model. Then, I would advice you to make the input field readonly to prevent any unexpected behaviour that will arise when someone changes the value.

data() {
  return {
    nasabah: {
      form: {
        salary: ''
      }
    }
  }
}
<div class="form-row">
<div class="form-group col-md-6">
   <input
     :value="formattedSalaryWithCurrency">  //use the value attribute
     readonly // make it readonly to prevent unexpected behaviour
   >
</div>
</div>
computed: { // a computed property will only re-compute when a dependency changes (in this case, this.nasabah.form.salary;)
  formattedSalaryWithCurrency() {
    return this.formatSalaryWithCurrency(this.nasabah.form.salary);
  }
}
methods: {
  formatSalaryWithCurrency(amount) {
    `Rp.${amount}`
  }
}
๐Ÿ‘คTony

1๐Ÿ‘

You should use Computed properties of Vue for that.

๐Ÿ‘คNasir Rabbani

Leave a comment