Vuejs.org: Does it support conditional render html attribute?

Created on 29 Feb 2016  路  6Comments  路  Source: vuejs/vuejs.org

v-if can conditional render html element, but I want to conditional render html attribute.How can I do that?
I prefer it like this:
<input :diabled="v-if {{flag}}" type="text">
if flag == true, rendered result is
<input diabled type="text">
if flag == false, rendered result is
<input type="text">
ps:diabled="false" is the same as disabled="true" :)

question

All 6 comments

:disabled="flag"

You can bind disabled attribute directly to an expression, in your case flag.

<input :disabled="flag" type="text"> 

You can see a live example here.

@hootlex and @simplesmiler have already answered. I close this.

That solution does not work for all HTML attributes. The one I am having trouble with is the <progress> element. The mere presence of the value attribute, regardless of its value, changes the behavior of the element. Without this feature I have to duplicate the entire tag and all the other attribute bindings just to toggle the presence of of the attribute.

If you want to put an attribute with a value.
:your-attr="flag ? 'attr-value' : false"

In some situations, the ability of conditional rendering of html attributes is still needed, for example, the breadcrumb, <el-breadcrumb-item :to="{ path: '/' }">home</el-breadcrumb-item>, if the "to" attribute isn't needed, it cannot be <el-breadcrumb-item :to="false">home</el-breadcrumb-item>, the "to" attribute is the same as <router-link>.

Yes, I can do this in another way, but it's much straightforward if there is a conditional rendering directive.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

andreasvirkus picture andreasvirkus  路  3Comments

daymos picture daymos  路  4Comments

nerdoc picture nerdoc  路  5Comments

estyh picture estyh  路  5Comments

ankitsinghaniyaz picture ankitsinghaniyaz  路  4Comments