Bound Unbound Expressions

Understanding Expressions in Lightning

Posted on Updated on

Hi All,

Yesterday I stuck with Expressions in Lightning. While solving this I came to know about different ways of using Expressions and different types of these.

An expression is any set of literal values, variables, sub-expressions, or operators that can be resolved to a single value. You can use expressions for dynamic output or passing values into components by assigning them to attributes. Expressions are also used to provide action methods for user interface events(i.e. OnClick,OnHover).

Bound Expressions

Bound expression syntax is: {!expression}. Curly braces with “!” bang operator is used for Bound expressions.

If you are making any change using Bound expression it will immediately reflected on your component. Bound expressions are used to reflect changes on your Lightning Component. Below are examples of Bound expressions in component.

	<!-- Accessing Fields and Related Objects -->
	<!-- Concatinate value with String -->
	<p>{!'Hello ' + v.msg}</p>
	<!-- Ternary Operator -->
	<ui:button aura:id="likeBtn" label="{!(v.likeId == null) ? 'Like It' : 'Unlike It'}" />
	<!-- Conditional Markup -->
	<aura:attribute name="edit" type="Boolean" default="true"/>
	<aura:if isTrue="{!v.edit}">
		<ui:button label="Edit"/>
	<aura:set attribute="else">
		You can’t edit this.
	<!-- Adding two items -->
	{!(v.item1) + (v.item2)}
	<!-- Using Global Variables -->
	<!-- Action Method -->
	<ui:button aura:id="likeBtn" label="Press Me" press="{!c.update}"/>
	<!-- Array Length -->
	{!v.myArray && v.myArray.length > 0}

Unbound Expressions

Unbound expression syntax is: {#expression}. Curly braces with “#” hash operator is used for Unbound expressions.

If you are making any change using unbound expression, it will not immediately reflect on your component. If you don’t want to reflect changes, you can use unbound expressions.

Note : Unbound expression changes are not reflected on component but they can be seen by javascript.

Below example show use of unbound expression in component.

    <aura:attribute name="truthy" type="Boolean"></aura:attribute>
    <aura:if isTrue="{#v.truthy}">
    <aura:set attribute="else">
    <ui:button aura:id="button" label="Check" press="{!c.update}"/>
    update : function(component, event, helper) {
		console.log('----> '+component.get("v.truthy"));
    rerender: function(cmp, helper) {
        return this.superRerender()

In above code when you click on “Check” button it will udpate unbound expression {!#v.truthy} in javascript controller. You will be able to see changes in console but they are not reflected on component.

Hope this will be helpful. Happy coding 🙂