Types of data binding with template in Vue.js
Last Updated :
04 Apr, 2022
In this article, we will see different ways to bind data with the template in Vue.js, along with understanding their implementation through the examples.
When we wish to utilize our variables or data within the template, we generally use the mustache syntax(double curly braces). However, this isn't permitted in Vue for HTML elements. In order to use the HTML elements, we must use a bind to connect our data to them. The Data Binding facilitates binding the data to the template, which helps to render to the view component. Vue provides its own set of directives used to dynamically bind our data to the templates.
Text Binding: It is used when we need to bind the content of any HTML in Vue. The syntax for using text binding is by using the v-text directive in any HTML element and a data property should be assigned to it. For instance, the data inside the <script> tag can be displayed with the help of the<template> tag that contains the v-text directive having the value that is to be rendered.
Example: In the below example, we have bound the data that is the name having the value, with the v-text directive, declared in the <div> tag.
App.vue
<template>
<div style="text-align: center;
width: 600px">
<h1 style="color: green">
GeeksforGeeks
</h1>
<b> Data Binding Type </b>
</div>
<div v-text="name"
style="text-align: center">
</div>
</template>
<script>
export default {
name: "App",
data() {
return {
name:
"This content is redered with the help of v-text directive",
};
},
};
</script>
Output:
We can bind our data ie., the name to the div tag using the v-text directive to dynamically bind our data to the HTML so that when we change the name to some other value, the changes will be reflected in the HTML template.
HTML Binding: It is the way of having some string data stored as any HTML to be used within any div tag. So the content of the div tag would be as per the HTML we have bound to it. The Syntax to use HTML binding is by using the v-html directive and specifying the data property to it. The v-html directive is used to update an element’s innerHTML with our data.
Example: In the below example, we have enclosed the string with the <b> tag. If we used v-text to bind the name data, it would show output along with the tags. But to actually apply the HTML element, we need v-html.
App.vue
<template>
<div style="text-align: center">
<h1 style="color: green">GeeksforGeeks</h1>
<b> Data Binding Type </b>
</div>
<br />
<div v-html="name"
style="text-align: center">
</div>
</template>
<script>
export default {
name: "App",
data() {
return {
name:
"
<p>Welcome to <b>GeeksforGeeks</b> Learning.
<br>A Computer Science portal for geeks.</p>
",
};
},
};
</script>
Output:
We can even bind the HTML to the template using the v-html directive which would apply the respective HTML tags to the bound data. We can even have anchor tags bound to the data.
Attributes Binding: It is used to dynamically bind properties to the attributes. We can't bind attributes using the mustache syntax " {{ }} " in Vue js. To set the value of attributes using our data, we need to use the v-bind directive. The Syntax for attributes binding is using v-bind: and then followed with respective attributes.
Note: The attributes to be bound using v-bind: should be declared in the data return statement otherwise it will throw an error.
Example: In the below example, we have bound the disabled property to the button tag, disabled specifies that the button is unclickable and cannot be used. We have bound it to the isDisabled data property which is a boolean value, set as true. So, we can use attribute binding to have a disabled submit button until a particular form is fully filled and toggle the data property to again enable it upon completing the form.
App.vue
<template>
<div style="text-align: center">
<h1 style="color: green">
GeeksforGeeks
</h1>
<b> Data Binding Type </b>
</div>
<br />
<div style="text-align: center">
<p>Subscribe to GBlog Channel</p>
<button v-bind:disabled="isDisabled">
Subscribe
</button>
</div>
</template>
<script>
export default {
name: "App",
data() {
return {
isDisabled: true,
};
},
};
</script>
Output:
We can bind the attributes of the HTML elements using the v-bind directive which would dynamically apply the properties to the respective HTML elements. For eg., we can bind the disabled attribute to data called isDisabled to toggle the state of the button.
Classes Binding: Like Attribute Binding, we can bind the classes used for CSS to data values using the classes binding. Any HTML tag can be assigned class names using the v-bind:class directive. The Syntax is to use v-bind: followed by class and set the value to any data variable. We can have multiple classes by adding to an array ( v-bind:class = " [ status, promoted ] ") which will apply both CSS classes to the HTML tag. By binding classes to data, we can programmatically change the UI based on events like changing the background color of any button on clicking it to make the UI interactive. To have conditional classes we can have any boolean value followed by && to have the functionality of setting the class based on some conditional statements.
Example: In the example below, we have conditionally set the class of a <h2> tag with isCondition followed by && where isCondition is true as declared in data, So the class which is enclosed in quotes ( promoted ) will be applied to the <h2> tag ( We have defined the promoted class in the styles ).
Note: The classes should be declared in the styles tag in the Vue file.
App.vue
<template>
<h2 v-bind:class="status">Status</h2>
<h2 v-bind:class="isCondition && 'promoted'">
Condition
</h2>
</template>
<script>
export default {
name: "App",
data() {
return {
status: "danger",
isCondition: "true",
};
},
};
</script>
<style scoped>
.danger {
color: red;
}
.promoted {
font-style: italic;
}
</style>
Output:

We can dynamically bind the classes for the HTML using the v-bind directive and change classes on basis of some functions or events. Conditional classes can also be used using the && along with a boolean data variable.
Styles Binding: Like CSS selectors, we can have the same styles for different HTML elements with the help of styles binding and even change the styles dynamically whenever required. As we know, for styles, we have 2 options either have inline styles or define them in the style tag but we can also link the styles to our data values using the Styles binding. The Syntax for styles binding is v-bind: followed by style and set the style to values in our data section. In the case of Inline Styling, we can have different data values and set them as we do in normal HTML. The major advantage of style binding is that we can change the data programmatically which would in turn change the styles applied as we can change the style for any class or id dynamically.
Example: In the example below, we set the style of <h2> tag with CSS properties linked with our data (color linked with someColor which has a value of orange, font-size linked with headerSize which has a value of 25 ). We can even create a data object with keys as CSS properties and values as shown in the <h2> tag where we linked the style with dataStyle which in turn is an object having color, fontSize, and padding properties.
App.vue
<template>
<h2
v-bind:style="{
color: someColor,
'font-size': headerSize + 'px',
}" >
Inline Styles
</h2>
<h2 v-bind:style="dataStyle">Data Style</h2>
</template>
<script>
export default {
name: "App",
data() {
return {
someColor: "orange",
headerSize: 25,
dataStyle: {
color: "orange",
fontSize: "50px",
padding: "20px",
},
};
},
};
</script>
Output:

We can also apply inline styles using the v-bind binding to dynamically bind the data along with any static values to our styles and change them according to any function or algorithm. Create styles object in the data and link the same to any HTML element to reduce writing the styles again and again for every element. If we want to separate styles to different data objects, apply multiple objects by inserting them inside [ ] brackets or arrays.
As we can see from the above that whenever we want to bind to any HTML attributes, we need to use v-bind: to do so ( for example v-bind:class, v-bind:id, v-bind: style, etc. ) which can become repetitive to have v-bind. So, we can replace v-bind: with only : ( for eg :class, :style, :id, etc. ).
Similar Reads
Vue.js Tutorial Vue.js is a progressive JavaScript framework for building user interfaces. It stands out for its simplicity, seamless integration with other libraries, and reactive data binding.Built on JavaScript for flexible and component-based development.Supports declarative rendering, reactivity, and two-way d
4 min read
Basics
Vue.js Introduction & InstallationVue JS is a JavaScript framework used to design and build user interfaces. It is one of the best frameworks for Single Page Web Applications. It is compatible with other libraries and extensions as well. In the development field, there may be so many issues that can not be solved by using a single l
2 min read
Vue.js InstancesA Vue.js application starts with a Vue instance. The instances object is the main object for our Vue App. It helps us to use Vue components in our application. A Vue instance uses the MVVM(Model-View-View-Model) pattern. The Vue constructor accepts a single JavaScript object called an options object
2 min read
Vue.js WatchersA Watcher in Vue.js is a special feature that allows one to watch a component and perform specified actions when the value of the component changes. It is a more generic way to observe and react to data changes in the Vue instance. Watchers are the most useful when used to perform asynchronous opera
3 min read
Vue.js MethodsA Vue method is an object associated with the Vue instance. Functions are defined inside the methods object. Methods are useful when you need to perform some action with v-on directive on an element to handle events. Functions defined inside the methods object can be further called for performing ac
2 min read
Vue.js Event ModifiersVue.js is a progressive javascript framework for developing web user interfaces. It is a performant, approachable, and versatile framework. We can create Single Page Applications as well as Full Stack applications. It is built on top of HTML, CSS, and Javascript which makes it easier for developers
3 min read
Vue.js DOM treeVue.js is a javascript framework that is used in building static as well as dynamic webpages and User Interfaces. It offers many lucrative features for developers to use. For example, virtual DOM, component architecture, directives, templates, event binding, and many more. Document Object Model (DOM
4 min read
How to write and use for loop in Vue js ?Vue.js is one of the best frameworks for JavaScript like ReactJS. The VueJS is used to design the user interface layer, it is easy to pick up for any developer. It is compatible with other libraries and extensions as well. If you want to create a single-page application then VueJS is the first choic
4 min read
Vue.js Two Way Binding ModelVue.js is a progressive framework for building user interfaces. The core library is focused on the view layer only and is easy to pick up and integrate with other libraries. Vue is also perfectly capable of powering sophisticated Single-Page Applications in combination with modern tooling and suppor
2 min read
Vue.js Reusing ComponentsVue.js is a progressive javascript framework for developing web user interfaces. It is a performant, approachable, and versatile framework. We can create Single Page Applications as well as Full Stack applications. It is built on top of HTML, CSS, and Javascript which makes it easier for developers
4 min read
Vue.js List RenderingVue.js is a progressive framework for building user interfaces. The core library is focused on the view layer only and is easy to pick up and integrate with other libraries. Vue is also perfectly capable of powering sophisticated Single-Page Applications in combination with modern tooling and suppor
4 min read
Vue.js List Rendering Mutation MethodsVue.js is a progressive framework for building user interfaces. The core library is focused on the view layer only and is easy to pick up and integrate with other libraries. Vue is also perfectly capable of powering sophisticated Single-Page Applications in combination with modern tooling and suppor
3 min read
Vue.js v-cloak DirectiveThe v-cloak directive is a Vue.js directive that will remain on the element until the associated Vue instance finishes compilation. Combined with CSS rules such as [v-cloak] { display: none }, this directive can be used to hide uncompiled mustache bindings until the Vue instance is ready. First, we
1 min read
Vue.js Passing Data to Child Components with PropsVue.js is a progressive javascript framework for developing web user interfaces. It is a performant, approachable, and versatile framework. We can create Single Page Applications as well as Full Stack applications. It is built on top of HTML, CSS, and Javascript which makes it easier for developers
3 min read
Vue.js Form Input Binding with Select optionVue.js is a progressive javascript framework for developing web user interfaces. It is a performant, approachable, and versatile framework. We can create Single Page Applications as well as Full Stack applications. It is built on top of HTML, CSS, and Javascript which makes it easier for developers
3 min read
Vue.js Dynamic ComponentsVue.js is a progressive javascript framework for developing web user interfaces. It is a performant, approachable, and versatile framework. We can create Single Page Applications as well as Full Stack applications. It is built on top of HTML, CSS, and Javascript which makes it easier for developers
3 min read
Vue.js Form Input Value BindingVue.js is a progressive javascript framework for developing web user interfaces. It is a performant, approachable, and versatile framework. We can create Single Page Applications as well as Full Stack applications. It is built on top of HTML, CSS, and Javascript which makes it easier for developers
5 min read
Vue.js Form Input Binding number ModifierVue.js is a progressive javascript framework for developing web user interfaces. It is a performant, approachable, and versatile framework. We can create Single Page Applications as well as Full Stack applications. It is built on top of HTML, CSS, and Javascript which makes it easier for developers
2 min read
Vue.js List Rendering v-for with v-ifVue.js is one of the best frameworks for JavaScript like React JS. The Vue JS is used to design the user interface layer, it is easy to pick up for any developer. It is compatible with other libraries and extensions as well. In order to repeat a task for a fixed amount of time, we make use of the f
3 min read
Vue.js List Rendering v-for with a RangeVue.js is one of the best frameworks for JavaScript like React JS. The Vue JS is used to design the user interface layer, it is easy to pick up for any developer. It is compatible with other libraries and extensions as well. The Vue JS is supported by all popular browsers like Chrome, Firefox, IE, S
3 min read
Vue.js Form Input Binding with Checkbox optionVue.js is a progressive javascript framework for developing web user interfaces. It is a versatile framework providing high speed and performance. We can create Single Page Applications as well as Full Stack applications.Input Binding is used to sync and maintain the state of form input elements wit
2 min read
Vue.js Form Input Binding with Multiline textVue.js is a progressive javascript framework for developing web user interfaces. It is a versatile framework providing high speed and performance. Â We can create Single Page Applications as well as Full Stack applications.Input Binding is used to sync and maintain the state of form input elements wi
2 min read
Vue.js Form Input Binding trim ModifierVue.js is a progressive javascript framework for developing web user interfaces. It is a versatile framework providing high speed and performance. Â We can create Single Page Applications as well as Full Stack applications.Input Binding is used to sync and maintain the state of form input elements wi
2 min read
Vue.js Form Input Binding with Radio OptionVue.js is a progressive javascript framework for developing web user interfaces. It is a versatile framework providing high speed and performance. We can create Single Page Applications as well as Full Stack applications.Input Binding is used to sync and maintain the state of form input elements wit
3 min read
Vue.js List Rendering v-for with an ObjectVue.js is a progressive framework for building user interfaces. The core library is focused on the view layer only and is easy to pick up and integrate with other libraries. Vue is also perfectly capable of powering sophisticated Single-Page Applications in combination with modern tooling and suppor
3 min read
Vue.js Render Function with h() ArgumentsVue.js is a progressive javascript framework for developing web user interfaces. It is a performant, approachable, and versatile framework. We can create Single Page Applications as well as Full Stack applications. It is built on top of HTML, CSS, and Javascript which makes it easier for developers
3 min read
Vue.js Composition API with TemplatesVue.js is a progressive javascript framework for developing web user interfaces. It is a versatile framework providing high speed and performance. We can create Single Page Applications as well as Full Stack applications. The Composition API allows author to Vue components using imported functions r
3 min read
Vue.js Event HandlingVue.js is an open-source ModelâViewâViewModel front-end JavaScript framework for building user interfaces and single-page applications. Vue.js has many own directives for DOM manipulation such as v-bind, v-on, v-model, etc. In this article, we will see how to handle the events and their Implementati
11 min read
Vue.js Declarative RenderingVue.js is a progressive framework for building user interfaces. The core library is focused on the view layer only and is easy to pick up and integrate with other libraries. Vue is also perfectly capable of powering sophisticated Single-Page Applications in combination with modern tooling and suppor
2 min read
Create a Hover effect in Vue.jsVue is a progressive framework for building user interfaces. The core library is focused on the view layer only and is easy to pick up and integrate with other libraries. Vue is also perfectly capable of powering sophisticated Single-Page Applications in combination with modern tooling and supportin
1 min read
Types of data binding with template in Vue.jsIn this article, we will see different ways to bind data with the template in Vue.js, along with understanding their implementation through the examples. When we wish to utilize our variables or data within the template, we generally use the mustache syntax(double curly braces). However, this isn't
8 min read
Vue.js Click EventVue.js is a progressive framework for building user interfaces. The core library is focused on the view layer only and is easy to pick up and integrate with other libraries. Vue is also perfectly capable of powering sophisticated Single-Page Applications in combination with modern tooling and suppor
2 min read
Pass data between components using Vue.js Event BusComponent communication in Vue.js can become complicated and messy at times using $emit and props. In real-world applications where the Component tree is nested and big, it is not convenient to pass data using this method as it will only increase the complexity of the application and make debugging
3 min read
Vue.js Render Functions Component VNodes creationVue.js is a progressive javascript framework for developing web user interfaces. It is a performant, approachable, and versatile framework. We can create Single Page Applications as well as Full Stack applications. It is built on top of HTML, CSS, and Javascript which makes it easier for developers
2 min read
Vue.js List Entering & Leaving TransitionsVue.js is a progressive javascript framework for developing web user interfaces. It is a versatile framework providing high speed and performance. Â We can create Single Page Applications as well as Full Stack applications.The Entering and Leaving Transitions are used to perform the animation on list
3 min read
Vue.js Composition API using ProvideVue.js is a progressive javascript framework for developing web user interfaces. It is a performant, approachable, and versatile framework. We can create Single Page Applications as well as Full Stack applications. It is built on top of HTML, CSS, and Javascript, making it easier for developers to i
3 min read
Vue.js List Move TransitionsVue.js is a progressive JavaScript framework for developing web user interfaces. It is a versatile framework providing high speed and performance. We can create Single Page Applications as well as Full Stack applications.The Vue.js List Move Transitions smooths the transition when an element is adde
3 min read
Vue.js Transitioning between the ComponentsVue.js is a progressive javascript framework for developing web user interfaces. It is a performant, approachable, and versatile framework. We can create Single Page Applications as well as Full Stack applications. It is built on top of HTML, CSS, and Javascript which makes it easier for developers
3 min read
REST API Call to Get Location Details in Vue.jsIn this article, we will know the REST API call to get the location details in VueJS, along with understanding its implementation through the examples.VueJS is one of the best frameworks for JavaScript like ReactJS. The VueJS is used to design the user interface layer, it is easy to pick up for any
7 min read
Transition
Directives
Vue.js Conditional RenderingVue.js is a progressive framework for building user interfaces. The core library is focused on the view layer only and is easy to pick up and integrate with other libraries. Vue is also perfectly capable of powering sophisticated Single-Page Applications in combination with modern tooling and suppor
2 min read
Vue.js List Rendering v-for with a ComponentVue.js is one of the best frameworks for JavaScript like ReactJS. The VueJS is used to design the user interface layer, it is easy to pick up for any developer. In order to repeat a task for a fixed amount of time, we make use of the for loop. The Components are used to build the combination of UI
3 min read
Vue.js List Rendering v-for on a <template>Vue.js is one of the best frameworks for JavaScript like React JS. The Vue JS is used to design the user interface layer, it is easy to pick up for any developer. It is compatible with other libraries and extensions as well. In order to repeat a task for a fixed amount of time, we make use of the fo
3 min read
Vue.js | v-if directiveThe v-if directive is a Vue.js directive used to toggle the display CSS property of a element with a condition. If the condition is true it will make it visible else it will make it invisible. First, we will create a div element with id as app and let's apply the v-if directive to this element with
2 min read
Vue.js | v-text directiveThe v-text directive is a Vue.js directive used to update a elementâs textContent with our data. It is just a nice alternative to Mustache syntax. First, we will create a div element with id as app and let's apply the v-text directive to this element with data as a message. Now we will create this m
1 min read
Vue.js | v-show directiveThe v-show directive is a Vue.js directive used to toggle the display CSS property of a element with our data via inline styles. If the data is true it will make it visible else it will make it invisible. First, we will create a div element with id as app and let's apply the v-show directive to this
2 min read
Vue.js | v-html directiveThe v-html directive is a Vue.js directive used to update a elementâs inner HTML with our data. This is what separates it from v-text which means while v-text accepts string and treats it as a string it will accept string and render it into HTML. First, we will create a div element with id as app an
2 min read
Vue.js v-on:click directiveThe v-on:click directive is a Vue.js directive used to add a click event listener to an element. First, we will create a div element with id as app and let's apply the v-on:click directive to a element. Further, we can execute a function when click even occurs.Syntax:v-on:click="function"Parameters:
1 min read
Vue.js v-once DirectiveThe v-once directive is a Vue.js directive that is used to avoid unwanted re-renders of an element. It treats the element as a static content after rendering it once for the first time. This improves performance as it does not have to be rendered again. First, we will create a div element with the i
1 min read
Vue.js v-on:click.ctrl DirectiveThe v-on:click.ctrl directive is a Vue.js directive used to add a click event listener to an element. While click directive triggers the event for all kinds of clicks, this directive only triggers the event when the ctrl key is pressed along with the click. First, we will create a div element with i
2 min read
Vue.js v-on:click.right DirectiveThe v-on:click.right directive is a Vue.js directive used to add a click event listener to an element. While click directive triggers the event for all kind of clicks, this directive only triggers the event when right key of mouse is clicked. First, we will create a div element with id as app and le
1 min read
Vue.js v-on:click.shift DirectiveThe v-on:click.shift directive is a Vue.js directive used to add a click event listener to an element. While click directive triggers the event for all kind of clicks, this directive only triggers the event when shift key is pressed along with the click. First, we will create a div element with id a
1 min read
Vue.js v-on:click.left DirectiveThe v-on:click.left directive is a Vue.js directive used to add a click event listener to an element. While click directive triggers the event for all kind of clicks, this directive only triggers the event when left key of mouse is clicked. First, we will create a div element with id as app and let'
1 min read
Vue.js v-on:click.alt DirectiveThe v-on:click.alt directive is a Vue.js directive used to add a click event listener to an element. While click directive triggers the event for all kind of clicks, this directive only triggers the event when alt key is pressed along with the click. First, we will create a div element with id as ap
1 min read
How a View-model works in Vue.js?Vue.js is a front-end progressive javascript framework for building User Interfaces ( UI ) and Single Page Applications. This framework focuses on Model â View â ViewModel architecture pattern that connects the View ( User Interface ) and the Model ( Data Objects ). Vue.js uses many built-in directi
4 min read
v-on:click.middle Directive in Vue.jsv-on:click.middle directive is a Vue.js directive used to add a click event listener to an element. While click directive triggers the event for all kind of clicks, this directive only triggers the event when middle key of mouse is clicked. First, we will create a div element with id as app and let'
1 min read
Vue.js v-pre DirectiveThe v-pre directive is a Vue.js directive used to skip compilation for this element and all its children. You can use this for displaying raw mustache tags. First, we will create a div element with id as app and let's apply the v-pre directive to an element. Further, we can use a heading element to
1 min read
Vue.js Form Input Binding lazy ModifierVue.js is a progressive javascript framework for developing web user interfaces. It is a performant, approachable, and versatile framework. We can create Single Page Applications as well as Full Stack applications. It is built on top of HTML, CSS, and Javascript which makes it easier for developers
3 min read
Data Conversion to KB, MB, GB, TB using Vue.js filtersIn this article, we are going to learn how to convert data to the given unit of data using filters in VueJS. Filters are a functionality provided by Vue components that let you apply formatting and transformations to any part of your template dynamic data. The filter property of the component is an
2 min read
Convert decimal point numbers to percentage using filters in Vue.jsVue is a progressive framework for building user interfaces. The core library is focused on the view layer only and is easy to pick up and integrate with other libraries. Vue is also perfectly capable of powering sophisticated Single-Page Applications in combination with modern tooling and supportin
2 min read
Capitalizing a string using filters in VueJSVue.js is a progressive framework for building user interfaces. The core library is focused on the view layer only and is easy to pick up and integrate with other libraries. Vue is also perfectly capable of powering sophisticated Single-Page Applications in combination with modern tooling and suppor
2 min read
Vue.js Placeholder using filtersVue.JS filters are a functionality provided by Vue components that let you apply formatting and transformations to any part of your template dynamic data. The filter property of the component is an object. A single filter is a function that accepts a value and returns another value. The returned val
3 min read
Truncate a String using filter in Vue.jsIn this article, we are going to learn how to truncate strings using filters in VueJS. Filters are a functionality provided by Vue components that let you apply formatting and transformations to any part of your template dynamic data. The filter property of the component is an object. A single filte
2 min read
Filters
Routing
What is the difference between one-way data flow and two-way data binding in vue.js? Vue.js is an open-source front-end JavaScript framework for building user interfaces and single-page applications. It follows Model-View-ViewModel (MVVM) architecture pattern. Vue.js has many in-built directives for DOM manipulation such as v-bind, v-on, v-model, etc. In this article, we will learn
7 min read
Difference Between