[SalesForce] Is it possible to set the value of an aura component to an expression dynamically

For example, is it possible to bind the label of a button to a "myButtonLabel" attribute by adding an expression string like in the following example?

  $A.createComponent(
        "ui:button",
        {
            "aura:id": "findableAuraId",
        //EXAMPLE:    
        "label": "{!v.myButtonLabel}",
        "press": cmp.getReference("c.handlePress")
        },
        function(newButton){
            //Add the new button to the body array
            if (cmp.isValid()) {
                var body = cmp.get("v.body");
                body.push(newButton);
                cmp.set("v.body", body);
            }
        }
    );

Best Answer

In Lightning, expressions come in two types:

  1. Propery Reference Values like {!v.myButtonLabel} which simply points to an attribute. For those, you can get the reference using cmp.getReference("v.myButtonLabel") and the values will be properly linked, i.e. the component will update when the value changes.

  2. Function Call Values like {!v.myAttr + 1} which contain an equation. All markup in a component is preprocessed on the server and Function Call Values are sent in a compiled version to the client. You can't create a new expression from JavaScript.