Search code examples
angularescapingstring-interpolation

escape character inside string interpolation Angular


I'm trying to do the following :

label="{{(item.nameGeneric.length === 0) ? 
    item.nameBrand : item.nameBrand (item.nameGeneric)}}"

The behaviour I expect is if item.nameGeneric is empty then only show nameBrand , otherwise show "namebrand (nameGeneric)". The thing is, parenthesis inside 'else' part of the condition are being interpreted by Angular. I wan't to escape them so my final text looks like "some brand (Generic brand) in that scenario "

BTW: that's a label attribute for a checkbox and I'm using Angular 8.


Solution

  • You can try to concat string values:

    label="{{(item.nameGeneric.length === 0) ? 
        item.nameBrand : (item.nameBrand + ' (' + item.nameGeneric + ' )') }}"