home
  • Blog
3.8
  • Getting Started
  • Tutorial
  • The Object Model
  • Templating
    • Templating Basics
    • Built-in Helpers
    • Conditionals
    • Displaying a List of Items
    • Displaying the Keys in an Object
    • Binding Element Attributes
    • Links
    • Actions
    • Input Helpers
    • Development Helpers
    • Writing Helpers
  • Components
  • Controllers
  • Routing
  • Ember Data
  • Addons and Dependencies
  • Testing
  • Configuration
  • Application Concerns
  • Ember Inspector
  • Contributing to Ember.js
  • Glossary
  • Reference
Old Guides - You are viewing the guides for Ember v3.8.0.
Go to v5.0.0

Binding Element Attributes

Edit pencil

In addition to normal text, you may also want to have your templates contain HTML elements whose attributes are bound to the controller.

For example, imagine your controller has a property that contains a URL to an image:

<div id="logo">
  <img src={{this.logoUrl}} alt="Logo">
</div>

This generates the following HTML:

<div id="logo">
  <img src="http://www.example.com/images/logo.png" alt="Logo">
</div>

If you use data binding with a Boolean value, it will add or remove the specified attribute. For example, given this template:

<input type="checkbox" disabled={{this.isAdministrator}}>

If isAdministrator is true, Handlebars will produce the following HTML element:

<input type="checkbox" disabled>

If isAdministrator is false, Handlebars will produce the following:

<input type="checkbox">

Adding Other Attributes (Including Data Attributes)

By default, components only accept a limited number of HTML attributes. This means that some uncommon but perfectly valid attributes, such as lang or custom data-* attributes must be specifically enabled. For example, this template:

{{#link-to "photos" data-toggle="dropdown" lang="es"}}Fotos{{/link-to}}

Will render the following HTML:

<a id="ember239" class="ember-view" href="#/photos">Fotos</a>

To enable support for these attributes, an attribute binding must be added for each specific attribute on the component. To do this, you can extend the appropriate components in your application. For example, for link-to you would create your own version of this component by extending Ember.LinkComponent

"app/components/link-to/component.js"
import LinkComponent from '@ember/routing/link-component';

export default LinkComponent.extend({
  attributeBindings: ['data-toggle', 'lang']
});

Now the same template above renders the following HTML:

<a id="ember239" class="ember-view" href="#/photos" data-toggle="dropdown" lang="es">Fotos</a>
left arrow
Displaying the Keys in an Object
Links
right arrow
On this page

  • Adding Other Attributes (Including Data Attributes)
Team Sponsors Security Legal Branding Community Guidelines
Twitter GitHub Discord Mastodon

If you want help you can contact us by email, open an issue, or get realtime help by joining the Ember Discord.

© Copyright 2023 - Tilde Inc.
Ember.js is free, open source and always will be.


Ember is generously supported by
blue