Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 Routerlink in for loop

trying create Menu with below code

<aside id="left-panel">
	<span class="minifyme"> <i class="fa fa-arrow-circle-left hit"></i>
	</span>
	<nav>
		<ul>
			<li *ngFor="#menu of menus" [class.open]="menu === selectedMenu"
				(click)="onSelect(menu)"><a [routerLink]="[menu.action]"> <i
					class={{menu.icon}}></i> <span class="menu-item-parent">{{menu.displayname}}</span>
					<b *ngIf="menu.childrens.length>0"> <em class="fa"
						[ngClass]="{'fa-minus-square-o': menu === selectedMenu, 'fa-plus-square-o': menu !== selectedMenu}"></em>
				</b>
			</a>

				<ul *ngIf="menu.childrens" [class.show]="menu === selectedMenu">
					<li *ngFor="#submenu of menu.childrens" (click)="onSelect(menu)">
						<a> {{submenu.displayname}} </a>
					</li>
				</ul></li>
		</ul>
	</nav>
</aside>

import {Component, OnInit} from 'angular2/core';
import {Router} from 'angular2/router';
import {RouteConfig,RouterLink, ROUTER_DIRECTIVES} from 'angular2/router';
import {MenuService} from './menu.service';
import {Menu} from './menu';


@Component({
  selector: 'my-menu',
  templateUrl: 'app/menu/menu.component.html',
  directives: [ROUTER_DIRECTIVES,RouterLink,RouteConfig]
})
export class MenuComponent implements OnInit {
  public menus: Menu[];
    public selectedMenu: Menu;
   
    constructor(private _menuService: MenuService) { }
    getMenus() {
        this._menuService.getMenus().then(menus => this.menus = menus);
    }
    ngOnInit() {
        this.getMenus();
    }
     onSelect(menu: Menu) { this.selectedMenu = menu; }
}

Getting following error, please suggest

EXCEPTION: No Directive annotation found on DecoratorFactoryBrowserDomAdapter.logError @ angular2.dev.js:23083 angular2.dev.js:23083 STACKTRACE:BrowserDomAdapter.logError @ angular2.dev.js:23083 angular2.dev.js:23083 Error: No Directive annotation found on DecoratorFactory at new BaseException (https://code.angularjs.org/2.0.0-beta.7/angular2.dev.js:7351:21) at DirectiveResolver.resolve (https://code.angularjs.org/2.0.0-beta.7/angular2.dev.js:6654:13) at RuntimeMetadataResolver.getDirectiveMetadata (https://code.angularjs.org/2.0.0-beta.7/angular2.dev.js:22303:47) at https://code.angularjs.org/2.0.0-beta.7/angular2.dev.js:22371:22 at Array.map (native) at Array.map (https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.33.3/es6-shim.min.js:10:16820) at RuntimeMetadataResolver.getViewDirectivesMetadata (https://code.angularjs.org/2.0.0-beta.7/angular2.dev.js:22370:25) at TemplateCompiler._compileNestedComponentRuntime (https://code.angularjs.org/2.0.0-beta.7/angular2.dev.js:24329:63) at https://code.angularjs.org/2.0.0-beta.7/angular2.dev.js:24314:26 at Array.forEach (native)

like image 780
Haneep CR Avatar asked Oct 30 '22 09:10

Haneep CR


1 Answers

You shouldn't use RouteConfig within the directives attribute of your component since it's not a directive but a decorator.

You can notice that the RouterLink directive is contained into ROUTER_DIRECTIVES.

Using this should be enough:

directives: [ ROUTER_DIRECTIVES ]

Moreover your need to configure routes on your component with the @RouteConfig decorator:

@Component({
  selector: 'my-menu',
  templateUrl: 'app/menu/menu.component.html',
  directives: [ROUTER_DIRECTIVES,RouterLink,RouteConfig]
})
@RouteConfig({
  (...)
})
export class MenuComponent implements OnInit {
  (...)
}

Have a look at this link to see how to configure this:

  • https://angular.io/docs/ts/latest/guide/router.html
like image 75
Thierry Templier Avatar answered Nov 11 '22 11:11

Thierry Templier