我在angular2中创建了一个指令。但是,不工作并返回错误模板解析。
我的指示:
import { Directive, Input } from '@angular/core';
import { TemplateRef, ViewContainerRef } from '@angular/core';
import {CookieService} from 'angular2-cookie/core';
@Directive({ selector: '[permission]' })
export class PermissionDirective {
constructor(
private templateRef: TemplateRef<any>,
private viewContainer: ViewContainerRef,
private _cookie:CookieService,
) {}
@Input() set permission(_user_type: any) {
this.viewContainer.clear();
// let _type = this._cookie.get("_type");
// if (_user_type == _type) {
// this.viewContainer.createEmbeddedView(this.templateRef);
// } else {
// this.viewContainer.clear();
// }
}
}
Html代码:
<li>
<a *permission="type" routerLink="/users/list"><i class="fa fa-users fa-fw"></i> Users</a>
</li>
控制台错误日志:
error_handler.js:45 EXCEPTION: Uncaught (in promise): Error: Template parse errors:
Can't bind to 'permission' since it isn't a known property of 'a'. ("
</li>
<li>
<a [ERROR ->][permission]="'2'" routerLink="/users/list"><i class="fa fa-users fa-fw"></i> Users</a>
"): LeftMenuComponent@38:19
答案 0 :(得分:1)
您是否将指令作为声明添加到应用模块?
import { PermissionDirective } from './permissions.directive';
@NgModule({
declarations: [PermissionDirective],
exports: [PermissionDirective]
})
export class PermissionsModule {}