Using component get and set methods

Attributes are declared in a component's markup. You can treat attributes as similar to Java variables or JavaScript variables. To extract the values from them, we use component.get(), and to set the values, we use component.set().

Let's suppose that you have defined an attribute for a component using aura:attribute, as follows:

<aura:component access="global">  <aura:attribute name="searchTerm" type="String" /></aura:component>

You can get the value of the attribute by using the following:

 component.get("v.searchTerm")

To set the value of the attribute, the syntax is as follows:

component.set("v.searchTerm",'Lightning')
Here, Lightning is the value of the attribute that we are setting.

Get Learning Salesforce Lightning Application Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.