1

I want to check if array in specific index is not null this is my code:

    *ngIf="routeService?.selectedPlaces?[index]!=null"

'routeService' is service that the component gets in its constractor and 'selectedPlaces' is array in the service. 'index' is property in the component.

I got this error:

    main.ts:13 Error: Template parse errors:
    Parser Error: Conditional expression routeService?.selectedPlaces?[index]!=null requires all 3 
    expressions at the end of the expression [routeService?.selectedPlaces?[index]!=null] in 
    ng:///PlaceAutocompleteFromDBComponent/template.html@5:29 ("-field class="example-full-width">

How can I check it?

1 Answer 1

3

In Typescript the elvis operator syntax is ?. In the second case, you have used only ?, which is not valid. You need to to use

*ngIf="routeService?.selectedPlaces && routeService.selectedPlaces[index]"
Sign up to request clarification or add additional context in comments.

3 Comments

The safe navigation operator ?. is defined in Angular templates, not in Typescript.
@ConnorsFan Check newer version of typescript - typescriptlang.org/docs/handbook/release-notes/…
Thanks for the info. I wasn't aware of that.

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.