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.

3
  • stackoverflow.com/questions/39535386/… Commented Aug 9, 2021 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. Commented Aug 9, 2021 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. Commented Aug 9, 2021 at 15:44

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

1 Comment

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?

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.