Search code examples
angularangular-ng-if

How to use Hyphen(-) between two strings in angular 9 ngIf condition


I have an If condition in html to check permission to access a particular page :

    *ngIf="permission?.product-report?.list_product_report"

product-report is a static name which gives directly to the condition

When I execute it, got an error as below:

    Property 'report' does not exist on type 'ProductComponent'.

Why this error occurs ? Someone please help me..


Solution

  • You might have to use an inelegant solution of mixing dot notation and bracket notation

    *ngIf="permission['product-report']?.list_product_report"
    

    The above solution might throw an error when 'product-report' is defined and list_product_report is not. I'm not sure of the behavior when safe navigation operator is appended to a property fetched using bracket notation. Here you might have to forgo the safe navigation operator and check each property manually.

    *ngIf="permission 
      && permission['product-report']
      && permission['product-report']['list_product_report']"