【问题标题】:Angular 8 - Cannot use directives - can't bind since it is not a known propertyAngular 8 - 无法使用指令 - 无法绑定,因为它不是已知属性
【发布时间】:2020-12-19 14:09:32
【问题描述】:

我创建了一个非常简单的指令(直接来自文档只是为了让它工作),但是,我不断收到这个错误:

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".

指令是我的共享模块:

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 {}

指令现在看起来像这样:

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;
        }
    }
}

它是这样称呼的:

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

我已经在声明和共享模块的导出中添加了指令,并且我已经将共享模块导入到我使用的每个其他模块中。我不知道还能做什么。

【问题讨论】:

    标签: javascript angular module angular-directive


    【解决方案1】:

    如果你使用没有'*'前缀的指令应该没问题:

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

    【讨论】:

      【解决方案2】:

      问题是选择器名称与输入名称不匹配,这样就可以了:

      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;
              }
          }
      }
      

      【讨论】:

      • 谢谢!我正在处理这个问题将近两天。我更改了我的库的前缀,我正在寻找其他所有东西而不是那个。
      猜你喜欢
      • 1970-01-01
      • 2018-12-03
      • 1970-01-01
      • 2019-11-04
      • 2021-05-30
      • 1970-01-01
      • 2020-09-10
      • 1970-01-01
      • 2017-06-21
      相关资源
      最近更新 更多