2

I'm trying to display one of two options with string interpolation in my html file based on the condition in my typescript file, if the variable cityName = '' is an empty string then interpolate {{currentLocationCity.name}} and if the variable cityName = '!null' is not an empty string then interpolate {{cityByCoordiantes.name}} , how can i achieve this, i can write two different divs for each result and use *ngIf to display one of them, but i think that there has to be a better way of achieving this.

2
  • 1
    Did you try using Conditional Operator inside your template? Commented Sep 9, 2021 at 12:23
  • 1
    Instead of nesting div's for conditionally rendering, you can use <ng-container *ngIf=""></ng-container> Commented Sep 9, 2021 at 12:23

1 Answer 1

4

You can use anything like div, ng-container, even ng-template for else condition, but you can simply use ternary operator like this:

<ng-container>
    {{ cityName ?  cityByCoordiantes.name : currentLocationCity.name}}
</ng-container>
Sign up to request clarification or add additional context in comments.

1 Comment

thanks it was exactly what i was looking for

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.