I'm updating one of our apps from rc4 to angular2.0.0 and I'm getting a template parse error at run time. Here is my view template:
<div *ngFor="let widget of widgets" class="col-xs-3 quick-link"> <a [routerLink]="['/Tile', widget.WidgetRoute.Feature, widget.WidgetRoute.SubFeature]"> <div class="tile-icon"> <span [className]="widget.IconCssClass"></span> <span *ngIf="widget.BadgeNumber > 0" class="badge">{{widget.BadgeNumber}}</span> </div> <h4>{{widget.Text}}</h4> </a> </div>
And the error is on the routerlink. Here's the error:
Can't bind to 'routerLink' since it isn't a known property of 'a'. (" <div *ngFor="let widget of widgets" class="col-xs-3 quick-link"> <a [ERROR ->][routerLink]="['/Tile', widget.WidgetRoute.Feature, widget.WidgetRoute.SubFeature]"> <di"): LdrComponent@4:19 Can't bind to 'ngIf' since it isn't a known property of 'span'. ("tile-icon"> <span [className]="widget.IconCssClass"></span> <span [ERROR ->]*ngIf="widget.BadgeNumber > 0" class="badge">{{widget.BadgeNumber}}</span> </div> "): LdrComponent@7:22
The routerLink doesn't seem malformed to me... What did I do wrong?
Solution. You need to import the routing module with this component to the root module of the feature.
the reason is that you probably did not include RouterModule to your @NgModule in imports section. Important thing is this needs to be included in every module where you are using Router link or any other router feature.
routerLinkActive is simply an indicator of whether this link represents the active route. @jessepinho - Tried it - [routerLinkActive]="'active'" will only work if you also have [routerLink] in the a-tag. If you had (click)="navitageTo('/route')" instead of [routerLink], and in that function, you called this.
routerLink is the attribute provided by angular to navigate to different components without reloading the page. Major difference between both is that href kills the state of the current page where routerLink doesnt lose the state of the page.
The problem is that you forgot to add RouterModule
to your NgModule
component. In the RC this was added to the @Component({directives: [ROUTER_DIRECTIVES]})
, however, this is now moved into @NgModule({ imports: [RouterModule]})
.
When you define your routes, one of the components that you will import will be the RouterModule
that you will use to call forRoot
or forChild
. When you import the route, this will be imported automatically.
So, you will get the RouterLink either this way, or via direct import into imports
property of @NgModule
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With