Open In App

Ember.js Component elementId Property

Ember.js is an open-source JavaScript framework used for developing large client-side web applications which are based on Model-View-Controller (MVC) architecture. Ember.js is one of the most widely used front-end application frameworks. It is made to speed up development and increase productivity. Currently, it is utilized by many websites, including Square, Discourse, Groupon, Linked In, Live Nation, Twitch, and Chipotle.

The elementId Property of the Component class is the property that is used to set the element of the component in the DOM. We can define the value of this property but the value should be unique. 



Syntax: 

elementId : value ; 

 



Parameters: 

Steps to Install and Run Ember.js:

Step 1: To run the following examples you will need to have an ember project with you. To create one, you will need to install ember-cli first. Write the below code in the terminal:

npm install ember-cli

Step 2: Now you can create the project by typing in the following piece of code:

ember new <project-name> --lang en

To start the server, type:

ember server

Example 1: In this example, we will create a component and bind the name ‘tutorial’ with the id attributes of the component element. the following code is to generate the route for this example:

ember generate route file1

app/components/first.js




import Component from '@glimmer/component';
import Ember from 'ember';
  
export default Ember.Component.extend({
    tagName: 'Section',
    elementId: 'tutorial',
})

app/components/first.hbs




{{page-title "Component ElementId"}}
  
<h3>{{yield}}</h3>

app/templates/file1.hbs




<h1>GeeksForGeeks</h1>
<h2>Hello Geeks keep reading and Grow up</h2>
<First>
    geeksforgeeks
</First>

app.css




#tutorial{
    font-familyTahoma, Geneva, Verdana, sans-serif;
    font-size: 25pt;
    color:#ae2549
}
  
h1{
    font-familyTahoma, Geneva, Verdana, sans-serif;
    font-size: 25pt;
    color:#2fd778
}

Output:

 

Example 2: In this example, we will define Element Id in a javascript file and define inline elementId to component element. Type of the following code to generate the route for this example:

ember generate route file2

app/components/second.js




import Component from '@glimmer/component';
import Ember from 'ember';
  
export default Ember.Component.extend({
    tagName: 'div',
    elementId: 'tutorial',
})

app/components/second.hbs




{{page-title "Component ElementId"}}
  
<h3>{{yield}}</h3>

app/templates/file2.hbs




<h1>GeeksForGeeks</h1>
<h2>Hello Geeks keep reading and Grow up</h2>
<Second @elementId="component">
    This is Component templates tag.
</Second>

app.css




#tutorial{
    font-familyTahoma, Geneva, Verdana, sans-serif;
    font-size: 25pt;
    color:#ae2549
}
  
h1{
    font-familyTahoma, Geneva, Verdana, sans-serif;
    font-size: 25pt;
    color:#2fd778
}
  
#component{
    font-family: -apple-system;
    font-size: 22pt;
    background-color:#ae2549;
}

Output:

output2

Reference: https://api.emberjs.com/ember/4.9/classes/Component/properties/elementId?anchor=elementId


Article Tags :