Expression Evaluation
Operators are a subset of those available in JavaScript, and evaluation order and precedence are generally the same as JavaScript. Parentheses enable you to ensure a specific evaluation order. What you may find surprising about expressions is how often they are evaluated. The framework notices when things change, and triggers re-rendering of any components that are affected. Dependencies are handled automatically. When a component is re-rendered, any expressions it uses will be re-evaluated.
Action Methods
Expressions are also used to provide action methods for user interface events: onclick, onhover, and any other component attributes beginning with "on".
Action methods must be assigned to attributes using an expression, for example {!c.theAction}. This expression assigns a reference to the controller function that handles the action.
Assigning action methods via expressions allows you to assign them conditionally, based on the state of the application or user interface. For more information, see Conditional Expressions.
<aura:component>
<aura:attribute name="liked" type="Boolean" default="true"/>
<lightning:button aura:id="likeBtn"
label="{!(v.liked) ? 'Like It' : 'Unlike It'}"
onclick="{!(v.liked) ? c.likeIt : c.unlikeIt}"
/>
</aura:component>
This button will show "Like It" for items that have not yet been liked, and clicking it will call the likeIt action method. Then the component will re-render, and the opposite user interface display and method assignment will be in place. Clicking a second time will unlike the item, and so on.
Action Methods with Lightning Web Components
If you try to use an action method with a Lightning web component, it doesn’t behave as expected because Lightning web components don’t support expressions the same way that Aura components do. To write an action method, assign a controller action and execute logic depending on a value.
<aura:component>
<aura:attribute name="liked" type="Boolean" default="true"/>
<c:lwcButton aura:id="likeBtn"
label="{!(v.liked) ? 'Like It' : 'Unlike It'}"
onclick="{!c.handleLikeButtonClick}"
/>
</aura:component>
({
handleLikeButtonClick: function (cmp) {
if (cmp.get('v.liked')) {
// like it logic
} else {
// unlike it logic
}
}
})