2

using vue.js, is it possible to set a custom attribute name, instead of attribute value? the below code doesn't seem to work. it will render the mustache

pseudo code:

<template v-for="item in list">

    <div myattr{{item.name}}="something">{{item.desc}}</div>

</template>

any help greatly appreciated.

1 Answer 1

1

You can do it with custom directive

HTML:

<div id="app">
  <li v-for="item in items">
    <div v-set-attribute="item">{{item.desc}}</div>
  </li>
</div>

JS:

Vue.directive('set-attribute', {
  update: function(item) {
    this.el.setAttribute(item.name, "something");
  }
});

Directive name must be prefixed with v- in html. Object provided as value ("item") is passed to update function of the directive.

JSFiddle

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

Comments

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.