Angular 10 错误 NG8002 无法绑定对象

Angular 10 error NG8002 can't bind object

每当我将以下代码行添加到 .html 文件时,我都会收到以下错误:

Error: src/app/department/show-dep/show-dep.component.html:22:11 - error NG8002: Can't bind to 'dep' since it isn't a known property of 'app-add-edit-dep'.

  1. If 'app-add-edit-dep' is an Angular component and it has 'dep' input, then verify that it is part of this module.
  2. If 'app-add-edit-dep' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.
  3. To allow any property add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component.

22 [dep]="dep" *ngIf="ActivateAddEditDepComp"> ~~~~~~~~~~~

src/app/department/show-dep/show-dep.component.ts:6:16 6 templateUrl: './show-dep.component.html', ~~~~~~~~~~~~~~~~~~~~~~~~~~~ Error occurs in the template of component ShowDepComponent.

<app-add-edit-dep
   [dep]="dep" *ngIf="ActivateAddEditDepComp">
</app-add-edit-dep>

添加此行之前一切正常。我将一个部门对象传递到我的添加编辑部门组件,以便它知道它是需要添加一个新部门还是编辑一个现有部门。我已经包含了我所处理的涉及相关部门对象的所有内容。

显示-dep.component.html

<!-- Button trigger modal -->
<button type="button" class="btn btn-primary float-right m-2" 
data-bs-toggle="modal" data-bs-target="#exampleModal"
(click)="addClick()"
data-backdrop="static" data-keyboard="false">
  Add Department
</button>

<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">{{ModalTitle}}</h5>
        <button type="button" class="btn-close" 
        data-bs-dismiss="modal" aria-label="Close"
        (click)="closeClick()" >
        </button>
      </div>
      <div class="modal-body">
        <app-add-edit-dep
          [dep]="dep" *ngIf="ActivateAddEditDepComp">
        </app-add-edit-dep>
      </div>
    </div>
  </div>
</div>

<table class = "table table-striped"> 
    <thead>
        <tr>
            <th>DepartmentID</th>
            <th>Department Name</th>
            <th>Options</th>
        </tr>
    </thead>
    <tbody>
        <tr *ngFor = "let dataItem of DepartmentList">
            <td>{{dataItem.DepartmentID}}</td>
            <td>{{dataItem.DepartmentName}}</td>
            <td>
                <button type="button" class = "btn btn-light mr-1">
                    Edit
                </button>
                <button type="button" class = "btn btn-light mr-1">
                    Delete
                </button>
            </td>
        </tr>
    </tbody>

</table>

显示-dep.component.ts

import { Component, OnInit } from '@angular/core';
import { SharedService } from 'src/app/shared.service';

@Component({
  selector: 'app-show-dep',
  templateUrl: './show-dep.component.html',
  styleUrls: ['./show-dep.component.css']
})
export class ShowDepComponent implements OnInit {

  constructor(private service:SharedService) { }

  DepartmentList:any=[];

  ModalTitle:string | undefined;
  ActivateAddEditDepComp:boolean = false;
  dep:any;

  ngOnInit(): void {
    this.refreshDepList();
  }

  addClick() {
    this.dep = {
      DepartmentId:0,
      DepartmentName:""
    }
    this.ModalTitle = "Add Department";
    this.ActivateAddEditDepComp = true;
  }

  closeClick() {
    this.ActivateAddEditDepComp = false;
    this.refreshDepList();
  }

  refreshDepList(){
    this.service.getDepList().subscribe(data=>{
      this.DepartmentList=data;
    });
  }

}

添加-编辑-dep.component.ts

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-add-edit-dep',
  templateUrl: './add-edit-dep.component.html',
  styleUrls: ['./add-edit-dep.component.css']
})
export class AddEditDepComponent implements OnInit {

  constructor() { }

  ngOnInit(): void {
  }

}

编辑:我在下面添加了 app.module.ts。

app.module.ts

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

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { DepartmentComponent } from './department/department.component';
import { ShowDepComponent } from './department/show-dep/show-dep.component';
import { AddEditDepComponent } from './department/add-edit-dep/add-edit-dep.component';
import { EmployeeComponent } from './employee/employee.component';
import { ShowEmpComponent } from './employee/show-emp/show-emp.component';
import { AddEditEmpComponent } from './employee/add-edit-emp/add-edit-emp.component';
import { SharedService } from './shared.service';

import {HttpClientModule} from '@angular/common/http';
import {FormsModule,ReactiveFormsModule} from '@angular/forms';

@NgModule({
  declarations: [
    AppComponent,
    DepartmentComponent,
    ShowDepComponent,
    AddEditDepComponent,
    EmployeeComponent,
    ShowEmpComponent,
    AddEditEmpComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    HttpClientModule,
    FormsModule,
    ReactiveFormsModule
  ],
  providers: [SharedService],
  bootstrap: [AppComponent]
})
export class AppModule { }

您的问题可以快速解决。只需在 declarations 数组前添加 schemas: [CUSTOM_ELEMENTS_SCHEMA]。理想情况下,您的应用程序应该能够识别 app-add-edit-dep 元素,因为该组件已在 declarations 数组中声明。确实,根据给定的信息,这是我现在能想到的唯一解决方案。