0

I am generation a side navigation, some of the objects have URL property and some don't (they are used for generating submenu).

my HTML template looks like this

<li *ngFor="let menu of navMenu;index as i" routerLink="{{menu.url}}">
....some stuff
</li>

I need that if the menu.url exist then do the routing , else do nothing , right now if menu.url don't exist it goes to / route.

2 Answers 2

2

Just add a check using *ngIf

<ng-container *ngFor="let menu of navMenu;index as i">
  <li *ngIf="menu.url"  routerLink="{{menu.url}}">
    ....some stuff
  </li>
</ng-container>
Sign up to request clarification or add additional context in comments.

5 Comments

But I want to render the li
Yea that can be done ... but is there someway I can set value of menu.url to something that it does not do the routing
you can use conditional routing as
<button [routerLink]="menu.url? {{menu.url}} : []"></button>
It worked , one update <button [routerLink]="menu.url? [menu.url] : []"></button>
2

Assuming that menu.url has an absolute path, just do something like:

<li *ngFor="let menu of navMenu;index as i" [routerLink]="menu.url ? [menu.url]: []">
....some stuff
</li>

3 Comments

Tried the conditional routing also but , the [] matches to "/" route. I need it to ignore the routing
What do you mean by "ignore the routing"? When menu.url is null, no link should be generated? (basically clicking on the li element would have no effect)
You are right , I was having some other issue thats why it was getting routed to the / , after correcting , it says at the same route thanks

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.