1

When I associate an attribute to a component, does the component not care about the base data type of the attribute ?

for eg: If I use an ui:outputtext component, I would assume lightning would error when I associate an Integer attribute type to value of an ui:outputtext . The below component compiles fine which to does not make sense.

Is this a feature or a bug?

When it comes to visualforce we have

apex:outputtext

apex:oupputfield

Since lightning has separate ouputnumber and ouputtext tags, I assumed it would error when I try to associate number to a text and text to a number.

Component:

<aura:component controller="integer_controller">
    <aura:attribute name="int_val" type="Integer"/>
    <aura:attribute name="string_val" type="String"/>
    
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    
    Output number : <ui:outputNumber aura:id="outNum" value="{!v.string_val}"/>
    <br/>
    output text: <ui:outputtext aura:id="outtext" value="{!v.int_val}"/>

</aura:component>

Controller:

({
    doInit: function(component, event, helper) {
        var servaction = component.get("c.intvalfromserver");
        var servaction2 = component.get("c.stringvalfromserver");
        servaction.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
              component.set("v.int_val",response.getReturnValue());
            }
        });
        $A.enqueueAction(servaction);
        
        servaction2.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
              component.set("v.string_val",response.getReturnValue());
            }
        });
        $A.enqueueAction(servaction2);
    }
})

Server side controller:

public class integer_controller {
    @AuraEnabled
    public static Integer intvalfromserver() {
        return 10;
    }
    
    @AuraEnabled
    public static String stringvalfromserver() {
        return 'abc';
    }
}

output:

Output number : abc

output text: 10

1 Answer 1

3

This was an intentional design decision by the component authors to not error.

You can see in the ui:outputText source code there's an explicit conditional block to create a text node when the input is a number instead of a string:

https://github.com/forcedotcom/aura/blob/master/aura-components/src/main/components/ui/outputText/outputTextHelper.js#L23

Similarly, in ui:outputNumber the input is treated the same if it's a string or a number:

https://github.com/forcedotcom/aura/blob/master/aura-components/src/main/components/ui/outputNumber/outputNumberRenderer.js#L22

1
  • Thanks for the reference, I could have never found that reference. So they do have validations to check if it is a number or string in the controller.
    – Rao
    Nov 15, 2016 at 22:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .