Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't bind to 'routerLink' since it isn't a known property of 'a' [duplicate]

Tags:

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?

like image 597
cobolstinks Avatar asked Sep 27 '16 21:09

cobolstinks


People also ask

Can't bind to routerLink since it isn't a known property?

Solution. You need to import the routing module with this component to the root module of the feature.

Can't bind to routerLink since it isn't a known property of a angular test?

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.

How do I know if my routerLink is active?

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.

Can we use href instead of routerLink?

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.


1 Answers

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.

like image 50
Husein Roncevic Avatar answered Oct 04 '22 18:10

Husein Roncevic