5

I want to check if a string contains a specific sequence of characters in some component markup using an expression. The Expression Functions Reference lists equals but nothing that looks suitable. And I don't see a way to hook in your own functions in expressions.

Can this be accomplished on the fly or am I stuck with finding some earlier point in the code to perform the check and store it in yet another attribute?

2 Answers 2

4

Honestly it's a good feature to have in LC but at the moment we are limited to the function expression as mentioned in here. So only option we have is, to do includes/indexOf... stuffs in the controller/helper and set the appropriate attribute value.

For eg:

controller.js

({
    init : function(component, event, helper) {
        let word = component.get("v.word"), sequenceToCheck = 'abc';
        if (work.indexOf(sequenceToCheck) > -1) {
            console.log('sequence exist');
        } else {
            console.log('sequence does not exist');
        }

        if (work.includes(sequenceToCheck)) {
            console.log('sequence exist');
        } else {
            console.log('sequence does not exist');
        }

    }
})
0
1

You can make a custom aura:if component who can manage extended conditions to test

quick sample ( not tested but should work ) :

<aura:component name="auraIfAdvanced">
<aura:handler name="init" value="{!this}" access="global" action="{!c.doInit}" />
<aura:attribute name="list" type="Object[]" />
<aura:attribute name="element" type="String" />
<aura:attribute name="condition" type="Boolean" default="false" />

<aura:if isTrue="{!v._condition}">
    {!v.body}
</aura:component>

controller:

doInit: function(component, event, helper) {
    if (component.get('v.list').indexOf(component.get('v.element)) > -0 )
         component.set('v.condition',true);
}

then you call it like

<c:auraIfAdvanced list="["a","b","c"]" element="b">
         WhateverU_NeedHere
 </c:auraIfAdvanced>
2
  • Thanks for the suggestion. I really need a term I can add into an expression rather than an on/off rendering component but that might sometimes be a solution. Don't think that init is the way to go here though as that only happens "after component construction but before rendering" not when later changes are made to attributes.
    – Keith C
    May 19, 2017 at 10:22
  • You can add a aura:change handler on élément attribute to recalculate condition. I agree this solution is way much interesting than expression but... it's just à workaround to lightning limitations ^^ May 20, 2017 at 14:44

You must log in to answer this question.

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