-2

I know this is some basic stuff for a lot of you but I'm trying to figure it out.

I saw this other post on doing ngif else conditionals with templates: How to use *ngIf else?.

<div *ngIf="isValid;then content else other_content">here is ignored</div>
  <ng-template #content>content here...</ng-template>
  <ng-template #other_content>other content here...</ng-template>

So, how do you get the template to know to translate the condition to its value.

e.g. someObj.someProperty If it exists then display it else display a text message.

<div *ngIf="someObj.someProperty;then content else other_content">here is ignored</div>
  <ng-template #content>someObj.someProperty</ng-template>
  <ng-template #other_content>not specified</ng-template>

FYI: I'm using https://stackblitz.com/

Why would I use that and not my desktop one may ask.

Easy my machine is locked down and I'm trying to get admin rights so I can install software on it. Try learning something with notepad and no way to run the code.

edjm
  • 4,830
  • 7
  • 36
  • 65
  • 2
    Have you tried wrapping `someObj.someProperty` in double curly brackets? Like this: `{{ someObj.someProperty }}`? – R. Richards Nov 01 '19 at 16:21
  • Yes I have. Also I'm using https://stackblitz.com/ , perhaps its an issue using a web page to do this? – edjm Nov 01 '19 at 16:22
  • wrote a stackblitz for you. enjoy. https://stackblitz.com/edit/angular-frs4pz?file=src/app/app.component.html – Stavm Nov 01 '19 at 16:25
  • Can't tell. You haven't shared the URL to the StackBlitz you're working with. – R. Richards Nov 01 '19 at 16:25
  • Sorry been on the help desk line for past 20 minuets trying to get someone to let me have some softare on this box. Here is the link: https://stackblitz.com/edit/angular-y5yush – edjm Nov 01 '19 at 16:48

1 Answers1

1

For this case, I wouldn't use *ngIf.

Consider:

<div> {{ someObj?.someProperty || 'other content to render' }}</div>

If someObj?.someProperty evaluates to a truthy (a non empty string), that will get rendered, otherwise the string 'other content to render' will be rendered.

C.OG
  • 6,236
  • 3
  • 20
  • 38