首页 > 解决方案 > Angular 11.0.3 无法绑定到 DIRECTIVE,因为它不是元素的已知属性

问题描述

我通过 Angular CLI 生成了新的 @Directive,它被导入到我的 app.module.ts 中:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppRoutingModule } from './app-routing.module';

import { AppComponent } from './app.component';
import { HttpClientModule } from '@angular/common/http';
import { InMemoryWebApiModule } from 'angular-in-memory-web-api';
import { DatosEnMemoriaService } from './datos-en-memoria.service';
import { ClonarDirective } from './investigaciones/marta/clonar.directive';
@NgModule({
  declarations: [
    AppComponent,
    ClonarDirective,
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    HttpClientModule,
    InMemoryWebApiModule.forRoot(DatosEnMemoriaService, {
      passThruUnknownUrl: true, post204: false, put204: false
  })
  ],  
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

我尝试在 mycomponent.html 中使用

<li *appClonar="3">
  <button (click)="this.router.navigateByUrl('/gesprodes');"  >
    Gesprodes
    <span>Inicia a aplicación de gesprodes</span>
  </button>
</li>

指令.ts:

import { Directive,Input,TemplateRef,ViewContainerRef } from '@angular/core';
 
@Directive({
  selector: '[appClonar]'
})
export class ClonarDirective {
 
  constructor(private tp:TemplateRef<any>,
              private vcr:ViewContainerRef) { }
 
    @Input() appClonar:number;
  ngOnInit(): void {
     this.vcr.createEmbeddedView(this.tp);
 
    console.log(this.appClonar);
    for(let i = 0; i<this.appClonar ;i++){
          this.vcr.createEmbeddedView(this.tp);
        }
  }
}

我得到了错误:

core.js:9813 NG0303:无法绑定到“appClonar”,因为它不是“li”的已知属性。

我尝试了几乎所有可能的更改,按照这个角度文档,一切都应该工作,但它没有。

我的版本角度:11.0.3 我的版本节点:12.0.0

任何帮助

标签: angularangular-directive

解决方案


你试过这个

<li [appClonar] >
</li>

推荐阅读