Code By Code – Lightning Framework – Chapter 10 – Access Attribute And Markup In Controller

Code By Code – Lightning Framework – Chapter 10 – Access Attribute And Markup In Controller

Chapter 10

Topics for this chapter:

  • Accessing Attributes In Controller
  • Get and Set methods
  • Find a component using aura:id

Accessing Attributes In Controller

From chapter 6, we have learned about attributes, their usage and how one can access these attributes in the component markup using expressions.

In this chapter, we will learn how you can access these attributes value in the controller file.
Below is the way to access or get the value of an attribute in the controller file:

component.get("v.showNew"); //showNew is the attribute name from CarSearchForm component file

To set attribute value from the controller file:

component.set("v.showNew", false);

Get markup component with aura:id in controller

aura:id defines the uniqueness of your HTML/Aura component in component markup. You can access these components in the controller file using find method:

component.find("carType");//carType is aura:id of aura:select element in CarSearchForm component

You can also access the attribute value of these components. For ex, if you want to access currently selected option of aura:select component, you can do this like below:

component.find("carType").get("v.value")

CarSearchForm Controller Code


Lightning Component Development Day4 - Use Attributes In Controller, System Events, Debugging, SOQL

Test your knowledge

Access Attribute And Markup In Controller

Next In This Guide

Manish Choudhari

I am a certified Salesforce Application & System Architect and Developer working on Salesforce Technology since 2014. Currently, I have 14 Salesforce certifications along with OCPJP (Oracle Certified Profession JavaSE6 Programmer) working in Salesforce.com Hyderabad as a Technical Engineer. Writing technical blogs, learning new technologies and frameworks and sharing knowledge is my hobby.

This Post Has One Comment

Leave a Reply

Close Menu