Search code examples
javascriptangularmoduleangular-directive

Angular 8 - Cannot use directives - can't bind since it is not a known property


I have created a very simple directives (straight from the documentation just to make it work), however, I keep getting this error:

Uncaught Error: Template parse errors:
Can't bind to 'appHasPermission' since it isn't a known property of 'button'. ("
                (click)="navigateToSingleCompany()"
                [translate]="'register.text.company'"
                [ERROR ->]*appHasPermission="true"
            >
                Company
"): ng:///DashboardModule/CompaniesOverviewCardComponent.html@41:4
Property binding appHasPermission not used by any directive on an embedded template. Make sure that the property name is spelled correctly and all directives are listed in the "@NgModule.declarations".

The directive is my shared module:

import { NgModule } from "@angular/core";
import { CommonModule } from "@angular/common";
import { NavMenuComponent } from "./layout/nav-menu/nav-menu.component";
import { CoreModule } from "../core/core.module";
import { RouterModule } from "@angular/router";
import { SideNavComponent } from "./layout/side-nav/side-nav.component";
import { RegisterCompanyFormComponent } from "./components/register-company-form/register-company-form.component";
import { ReactiveFormsModule } from "@angular/forms";
import { ErrorCardComponent } from "./components/error-card/error-card.component";
import { HasPermissionDirective } from "./directives/has-permission.directive";

@NgModule({
    declarations: [
        NavMenuComponent,
        SideNavComponent,
        RegisterCompanyFormComponent,
        ErrorCardComponent,
        HasPermissionDirective,
    ],
    imports: [CommonModule, CoreModule, RouterModule, ReactiveFormsModule],
    exports: [
        NavMenuComponent,
        SideNavComponent,
        RegisterCompanyFormComponent,
        ErrorCardComponent,
        CommonModule,
        HasPermissionDirective,
    ],
})
export class SharedModule {}

The directive simply looks like this now:

import { Directive, Input, TemplateRef, ViewContainerRef } from "@angular/core";

@Directive({
    selector: "[appHasPermission]",
})
export class HasPermissionDirective {
    private hasView = false;

    constructor(
        private templateRef: TemplateRef<any>,
        private viewContainer: ViewContainerRef
    ) {}

    @Input() set hasPermission(condition: boolean) {
        if (!condition && !this.hasView) {
            this.viewContainer.createEmbeddedView(this.templateRef);
            this.hasView = true;
        } else if (condition && this.hasView) {
            this.viewContainer.clear();
            this.hasView = false;
        }
    }
}

And it is called like this:

        <button
            class="btn btn-dark"
            (click)="navigateToSingleCompany()"
            [translate]="'register.text.company'"
            *appHasPermission="true"
        >
            Company
        </button>

I have added the directive in the declarations and in the exports within the shared module, and I have imported the shared module into every other module that I use. I am not sure what else to do.


Solution

  • The problem was that the selector name did not match the input name, this way made it work:

    import { Directive, Input, TemplateRef, ViewContainerRef } from "@angular/core";
    
    @Directive({
        selector: "[appHasPermission]",
    })
    export class HasPermissionDirective {
        private hasView = false;
    
        constructor(
            private templateRef: TemplateRef<any>,
            private viewContainer: ViewContainerRef
        ) {}
    
        @Input() set appHasPermission(condition: boolean) {
            if (!condition && !this.hasView) {
                this.viewContainer.createEmbeddedView(this.templateRef);
                this.hasView = true;
            } else if (condition && this.hasView) {
                this.viewContainer.clear();
                this.hasView = false;
            }
        }
    }