0

I am using ngFor to create a list of items. In production, my app had a bug and stopped the list at an item that caused an exception during the execution of my template.

<div *ngFor="let foo of foos">
    <p>{{foo.this.attribute.doesnt.exist}}</p>
</div>

Is there a way to enable a production mode, in which Angular simply skips this list entry? I prefer to have a list not being present in the list rather than breaking it mid-way and destroying the layout.

HelloWorld
  • 2,392
  • 3
  • 31
  • 68
  • https://stackoverflow.com/questions/39535386/how-to-check-if-angular-application-running-in-production-or-development-mode – Muhammet Can TONBUL Aug 09 '21 at 13:28
  • 1
    What exceptions are you referring to? If they're all just missing attribute exceptions then Oussail's answer is probably your best bet. – RobertAKARobin Aug 09 '21 at 14:41
  • 1
    do not put your business in your HTML code! then you will be just worrying about some basic errors like undefined objects that could handle by the "?" operator as @Oussail said. – Mazdak Aug 09 '21 at 15:44

1 Answers1

2

Yes, You can add the sign ? to the properties which you don't know if they are defined or not.

Example :

<div *ngFor="let foo of foos">
    <p>{{foo?.this?.attribute?.doesnt?.exist}}</p>
</div>
Oussail
  • 2,200
  • 11
  • 24
  • Thanks! That's really helpful, but `foo?.this?.attribute?.doesnt?.exist` was just an example. Sometimes there is a variety of possible other errors. Is there another way to make this work for all types of exceptions? – HelloWorld Aug 09 '21 at 13:38