7

This works:

<input v-model="project.name" :readonly="isReadOnly" :disabled="isReadOnly">

Is there a way to make below code work?

<input v-model="project.name" {{ readOnlyAttr }} >
<input v-model="project.name" {{ isReadOnly : 'readonly disabled' ? ''}}>

Script as follows:

<script>
  export default {
    props: {
      isReadOnly: {
        default: ture,
        type: Boolean
      }
    },
    data () {
      return {
        readOnlyAttr: 'readonly disabled'
      }
    }
  }
</script>
2
  • not sure i understood - u want to group toghter all the attributes in a single object to pass? Commented Oct 14, 2017 at 8:06
  • @LiranC yes, as simple as that, just group the attributes and apply to the element. Commented Oct 14, 2017 at 8:20

1 Answer 1

10

v-bind is your friend here. Because you want the attributes to be computed, I created a computed method to build the object everytime there is a change to isReadOnly value.

When you want to bind statically group of attributes, you can use the data method.

<template>
  <div>
    <input v-model="project.name" v-bind="readOnlyAttributes">
  </div>
</template>
<script>

export default {
  name: 'example',
  computed: {
    readOnlyAttributes() {
      return {
        readonly: this.isReadOnly,
        disabled: this.isReadOnly ? 'readonly' : ''
      }
    },
    isReadOnly() {
      // returning always true for the example
      return true;
    }
  }
}

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

1 Comment

thanks for your help :) Here is what I did: This is how I call the form: <base-form :project="project" :is-read-only="true"></base-form> Here is my template: <input v-model="project.name" v-bind="readOnlyAttributes"> <input v-model="project.place" v-bind="readOnlyAttributes"> The script looks like this now as you suggested, It's works :) props: { isReadOnly: { default: false, type: Boolean } }, computed: { readOnlyAttributes () { return { readonly: this.isReadOnly, disabled: this.isReadOnly } } }

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.