[英]Error message : can't bind to xxx since it isn't a known property of yyy when calling angular component
我在我的 Angular 项目中创建了一个名为 useraccount-type 的共享组件。 我将它用于表单,但出现以下错误消息:
无法绑定到“selectedType”,因为它不是“app-useraccount-type”的已知属性。
html 部分如下所示:
<div [formGroup]="myForm">
<select class="form-select form-control" id="useraccountType" formControlName="useraccountType" [(ngModel)]="selectedType">
<option *ngFor="let t of types" [ngValue]="t.description" >{{t.description}}</option>
</select>
</div>
ts 部分看起来是这样的:
import { Component, Input, OnInit } from '@angular/core';
import { ControlContainer, FormGroup } from '@angular/forms';
import { ErrorHandlerService } from 'src/app/services/error-handler.service';
import { RepositoryService } from 'src/app/services/repository.service';
import { environment } from 'src/environments/environment';
// Classes
import { Useraccounttype } from './../../../model/useraccounttype.model';
@Component({
selector: 'app-useraccount-type',
templateUrl: './useraccount-type.component.html',
styleUrls: ['./useraccount-type.component.css']
})
export class UseraccountTypeComponent implements OnInit {
public errorMessage: string = '';
@Input() public types: Useraccounttype[];
@Input() public selectedType?: string ="";
@Input() public myForm: FormGroup;
constructor(private repository: RepositoryService, private controlContainer: ControlContainer, private errorHandler: ErrorHandlerService) { }
ngOnInit(): void {
this.myForm = <FormGroup>this.controlContainer.control;
this.getUserAccountTypes();
console.log('Selected type' + this.selectedType);
}
getUserAccountTypes = () => {
let apiType: string = environment.apiAddress + 'UserAccountType';
this.repository.getData(apiType)
.subscribe(response => {
this.types = response as Useraccounttype[];
},
(error) => {
this.errorHandler.handleError(error);
this.errorMessage = this.errorHandler.errorMessage;
})
}
}
该组件以如下形式使用:
<form [formGroup]="myForm" autocomplete="off" novalidate>
...
<div class="col-md-12">
<app-useraccount-type name="useraccountType" id="useraccountType" [formGroup]="myForm" [selectedType]="user?.userAccountType?.shortDescription"></app-useraccount-type>
</div>
...
</form>
错误信息在这部分:
[selectedType]="user?.userAccountType?.shortDescription"
有人知道这个问题吗?
在此先感谢您的帮助。
PS:有关信息,我在 app.module.ts 中导入了 FormModule 和 ReactiveFormsModule
该错误意味着组件<app-useraccount-type>
没有@Input() selectedType
好的问题终于解决了。 我忘记在 useraccount.module.ts 中声明 useraccounttype 组件(在声明部分和导出部分)。 谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.