Join the Stack Overflow Community
Stack Overflow is a community of 6.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I'm using vuejs and I wanna know how to have control on inputs (add disabled attribute when necessary). Is there any way to add dynamically attribute in vuejs ? Below my Textfield component :

    <template>
     <input type="text" placeholder="{{ placeholder }}" v-model="value">
    </template>
    <script>
    export default  {
      props: {
       disabled: {type: Boolean, default: false},
       placeholder: {type: String, default: ""},
       value: {twoWay: true, default: ""}
      }
     }
    </script>

Usage :

<textfield placeholder="Name" value.sync="el.name" :disabled="true"></textfield>
share|improve this question
up vote 5 down vote accepted

You can bind it to a variable using v:bind:disabled="foo" or :disabled="foo" for short:

<textfield label="Name" value.sync="el.name" :disabled="myVar">

Then in Vue you can just set this.myVar = true and it will disable the input.

Edit: add this to your template:

<template>
  <input type="text" :disabled="disabled" placeholder="{{ placeholder }}" v-model="value">
</template>
share|improve this answer
    
Thanks for your reply. As newbie in VueJs, can you please help me to make it work. (Code updated) – Maria Minh Aug 31 '16 at 11:03
    
Your code is fine, you just need to pass a variable from your parent component that is true or false, depending on if you want it to be disabled. So <textfield :disabled="variable"> – Jeff Aug 31 '16 at 11:07
    
see the edit - that is what binds the disabled attribute to the input field – Jeff Aug 31 '16 at 11:28
    
Oh thanks, it works now ;) – Maria Minh Aug 31 '16 at 11:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.