Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are custom attribute bindings possible with Vue templates?

I'm trying to bind a custom attribute value in my Vue template. How can I do this?

(EDIT: The following code actually binds correctly. A third party library (Foundation) was interfering with the binding. Leaving the question up as it may be useful to others.

<template>
    <span v-bind="{ 'aria-controls': inputControlId }"></span>
    <input v-bind="{ 'id': inputControlId }">
</template>

<script lang="ts">

    import Vue from 'vue';
    import Component from 'vue-class-component';

    @Component
    export default class Slider extends Vue {
       inputControlId = "TheBourneId";
    }
}
</script>
like image 413
pulekies Avatar asked Jun 22 '17 20:06

pulekies


People also ask

What is attribute binding in Vue?

When we talk about data binding in Vue, we mean that the place where it is used or displayed in the template is directly linked, or bound to the source of the data, which is the data object inside the Vue instance. In other words, the host of the data is linked to the target of the data.

How do Vue templates work?

Vue uses an HTML-based template syntax that allows you to declaratively bind the rendered DOM to the underlying component instance's data. All Vue templates are syntactically valid HTML that can be parsed by spec-compliant browsers and HTML parsers.

What is the difference between v-model and V-bind?

In simple words v-model is for two way bindings means: if you change input value, the bound data will be changed and vice versa. but v-bind:value is called one way binding that means: you can change input value by changing bound data but you can't change bound data by changing input value through the element.


1 Answers

The common syntax for binding attributes is

<template>
    <span v-bind:aria-controls="inputControlId"></span>
    <input v-bind:id="inputControlId">
</template>

There is also a shorthand.

<template>
    <span :aria-controls="inputControlId"></span>
    <input :id="inputControlId">
</template>

You can bind multiple properties at once using the syntax in your question, it's just not commonly used outside class or style, especially for single attributes.

It sounds like the real issue was your CSS framework.

like image 176
Bert Avatar answered Oct 02 '22 00:10

Bert