我创建了一个有角度的模板驱动形式。但由于无法读取未定义的属性“单位”,我遇到了错误。我也创建了模型。并在ts文件中也定义了。仍然出现此错误。请提供任何帮助。
user-choice.component.html
<form name="form" #f="ngForm" class="form-horizontal">
<div class="border mt-2 p-2 pt-4">
<div class="form-row">
<div class="form-group col-md-12">
<label for="units">
Please specify the number of units being used: </label>
<input type="number" min="1" max="12" class="col-sm-2 pr-0 pl-0 col-form-label" [(ngModel)]="userChoice.units.value" name="units" >
</div>
</div>
</form>
user-choice.component.ts
import { Component, OnInit ,Input} from '@angular/core';
import { UserChoice } from './user-choice.model';
@Component({
selector: 'app-user-choice',
templateUrl: './user-choice.component.html',
styleUrls: ['./user-choice.component.css']
})
export class UserChoice implements OnInit {
constructor() { }
userChoiceModel: UserChoiceModel;
@Input() unitBase:any;
ngOnInit() {
}
}
user-choice.model.ts
import { BasicSearchModelI } from '../../../../../core/base';
export class UserChoiceModel {
units: BasicSearchModelI = {
value: ''
};
qty: BasicSearchModelI = {
value: ''
};
desc: BasicSearchModelI = {
value: ''
};
}
答案 0 :(得分:1)
您的模型名称是userChoiceModel
,但是在模板中,您正在使用userChoice
。您需要将ngModel
更新为[(ngModel)]="userChoiceModel.units.value"
。
现在,您需要做的就是在组件中初始化userChoiceModel
。您所做的只是给它一个类型,但是它的值仍然是undefined
。您可以像这样初始化它
userChoiceModel: UserChoiceModel = new UserChoiceModel();