Map Attribute in Lightning Component
Biswajeet
December 13, 2017 No Comments on Map Attribute in Lightning Component
Sample Component:
<!--Sample.cmp--> <aura:component> <aura:attribute name="map" type="Map" default="{str1:null,str2:null,obj:null}"/> <aura:attribute name="str1" type="String" default="{!v.map.str1}"/> <aura:attribute name="str2" type="String" default="{!v.map.str2}"/> <aura:attribute name="obj" type="Contact" default="{!v.map.obj}"/> <aura:handler name="init" value="{!this}" action="{!c.doInit}"/> <p><b>{!v.map.str1}</b></p> <p><b>{!v.map.obj.FirstName}</b></p> <p><b>{!v.map.str2}</b></p> <p><b>{!v.map.obj.LastName}</b></p> </aura:component>
Sample Component JS Controller:
({ doInit: function(cmp, event, helper) { //Set the attribute value. var map = cmp.get("v.map"); map['str1'] = 'FirstName'; map['str2'] = 'Lastname'; var obj = {"FirstName":"Biswajeet","LastName":'Samal'}; map['obj'] = obj; cmp.set("v.map", map); } })
Lightning App:
<aura:application extends="force:slds"> <c:Example /> </aura:application>