How to access apex class property in lightning component

Lightning Component

Prerequisites : basic understanding of Lightning Component and Apex programming

classController.apxc

  • First we create  component controller class .
  • It’s have 3 {get ;set;}  property variable on line number 5,6 and 7, with @AuraEnabled annotation .
  • The @AuraEnabled annotation enables client- and server-side access to an apex controller method and controller property. Providing this annotation makes your methods and properties available to your Lightning components.
  • In this controller we have a single method which return type is class type .

Test.cmp

 

    • In our test component we have a aura:attribute which type is class Type.
    • On the component initialize  call initClass() method and set return value in aura:attribute.
    • And finally access class property in component by {! v.objClassController.PropertyName }

TestController.js

 

TestApp.app

output-:

apex class properties
Like our facebook page for new post updates.?

(Visited 7,737 times, 1 visits today)

9 comments

  • Hi I have a issue using apex class property in lightning component, I used this to replicate the custom javascript button in classic to lightning. please help me

  • How can this be acheived in Opposite direction? passing a value from Component to an Auraenabled method in Server and using that value for other dependent methods in same class

  • It would have been much better if minimal code were to have been put up. Maybe one to get and one to set. Also, the standard coding conventions have not been followed. If I were to have known these, maybe, I would have started with Apex class with a string and passing that to Lightning and displaying it. Not sure why aura iterators, getters, setters, and all others are in one page without much explanation. Works for copy paste. But, not easy for learners.

  • This is great, thank you for sharing. This works for me to initialize the wrapper class and preload values. What if I want to update the class property values when an event occurs after the page is initialized? Ive tried this…

    component.set(‘v.objClassController.DeveloperName’, ‘John Brown’);

    But received…

    Action failed: c:Component_Name$controller$updateName [Cannot read property ‘DeveloperName’ of null]

    Surely I don’t have to do a callback each time I want to update the values. Thoughts?

    Thanks ahead of time.

     

Leave a Reply