0

I'm facing this sweet issue since three days. Let start with this single HTML code that is making

<mat-list-item    *ngFor="let setting of SideBarLinks"
                   [ngStyle]="{'display': setting['_value'] === true ? 'block':'none'}"   routerLink="[{{setting.link}}]">
        <mat-icon mat-list-icon>{{setting['icon']}}</mat-icon>
        {{setting.name}}
 </mat-list-item>

the problem is that how many time i click on it, it makes nothing. I tried to automate the list in my side bar using a separated JSON linked to the application. I use AngularX for about a year and i kindly know that routerLink directive doesn't work on every component that exists. but when I use it as a Single Component (without the *ngFor)

Here is the image that may help you to understand

2 Answers2

1

proper syntax:

[routerLink]="[setting.link]">

without the [] means it's a value and you can use the {{ }} syntax to make it dynamic, with the brackets means it's an expression. Since you're using the routerLink array syntax, I'm assuming you need it, which means you need an expression.

bryan60
  • 28,215
  • 4
  • 48
  • 65
  • I tried your your answer, but as I thought i didn't work... when I inspect the component when the webapp is rendered the list item is displayed without the routerLink... It looks like the list item lost it routerLink attribute, and displayed like a non link item – Alexandre Saison Oct 31 '17 at 19:42
  • i don't think you understand how routerlinks work. They're not supposed to be on the element when you inspect it with dev tools. If you put together a plunkr I will gladly prove that this works and show you that you're going wrong elsewhere. Your statement that " i kindly know that routerLink directive doesn't work on every component that exists" tells me that you just don't know the inner workings of the angular router well enough, becuase it DOES work for every component that exists. – bryan60 Nov 01 '17 at 13:01
  • Wait,I just understood your question... You're trying to ACTUALLY make the material list item a link? Of course that doesn't work. Router links are only supported on anchors and buttons. You need to make an anchor tag. Duh. – bryan60 Nov 01 '17 at 13:07
  • Hell yeah ... sry if my question was awfully asked... I know that but in some strange way it works for single material list item for example my settings list item works , but it seems that is because it's outside of an ngFor – Alexandre Saison Nov 01 '17 at 13:13
  • it has nothing to do with ngFor, it's because you in this case you need an expression, and the true directive binding (with the brackets) isn't working because it's only supported on anchor tags and buttons, just put an anchor tag around the inner items and put the directive there. – bryan60 Nov 01 '17 at 13:17
  • i finally use the anchor tag idea, i had allready test it, i knew it will work, but i was wanted to get it work the same way that on the single mat-list-item – Alexandre Saison Nov 02 '17 at 09:27
0

Try creating a method

getRouerLink(link) {
  return [link];
}

and then call it in your *ngFor like routerLink = "getRouterLink(setting.link)"

komron
  • 2,267
  • 2
  • 17
  • 26