8

In Visualforce you can use custom html attributes like this https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_html_features_pass_through_attributes.htm

<apex:outputText html-data-myAttr="myValue" ... />

Now in a Lightning-Component trying the same

<ui:outputText html-data-myAttr="myValue" ... />

I got only

The attribute "html-data-myAttr" was not found on the COMPONENT markup://ui:outputText: Source

How to set custom attributes in Lightning?

  • 1
    I haven't had a chance to do this myself, but you may find your answer in the PDF version of the Aura docs here: github.com/forcedotcom/aura/raw/master/aura_oss.pdf – Mark Pond Oct 28 '15 at 15:32
  • Thanks @MarkPond - there is something on passing attributes, but only for custom components (page 20). I was looking for passing through undefined attributes straight into the generated output-markup on Standard Components like ui:outputText – Uwe Heim Oct 28 '15 at 16:17
9

data- custom attributes are only supported by HTML components in Lightning. The same is true for VF - the apex:outputText component was written to specifically support custom HTML attribute pass through its not something any arbitrary component supports or would even make sense since many components are dealing with constructs that do not Mao to a single DOM element.

In lightning you can decorate any low level aura:html backed component - even simple <div>s and <span>s etc are actually represented as components in Lightning (they get turned into component refs that look like <aura:html tag="div"> by the Lightning compiler).

<div data-myAtt="blah"> 

Will get passed through to the DOM unmodified.

| improve this answer | |
  • Thanks Doug, that makes sense. I'll try if I can use a simple span instead of ui:outputText in my specific situation to wire-up my events. My goal is to get the custom attributes value in the handler. – Uwe Heim Oct 29 '15 at 10:23
  • 2
    Given the locker service prevents us from accessing DOM elements beyond that which fires an event on a lightning component (I tested and confirmed this), how can we pass custom data attributes to the controller from a ui:whatever component ? – CloudHugger Aug 11 '16 at 23:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.