[英]Data binding with Angular (4.0.0) and Material 2.0.0-beta.8
I'm new to Typescript and overall WebDev.我是 Typescript 和整个 WebDev 的新手。 Currently, I want to use md-select to bind data to a form that is submitted to a backend, but my current implementation breaks the page.
目前,我想使用 md-select 将数据绑定到提交到后端的表单,但我当前的实现打破了页面。 The HTML looks as follows.
HTML 如下所示。
<md-input-container class="home__input-container">
<md-select placeholder="sex | male/female" [(ngModel)]="patient.gender" name="gender">
<md-option *ngFor="let gender of genders" [value]="gender">
{{gender}}
</md-option>
</md-select>
</md-input-container>
I am importing most possible modules (MdSelect, Forms, etc.) as shared.modules file in appcomponent.我正在将大多数可能的模块(MdSelect、Forms 等)作为 shared.modules 文件导入 appcomponent 中。 The select is used in one of the other components.
select 用于其他组件之一。 Here I define the genders string.
在这里,我定义了性别字符串。
public genders: string[] = ["male","female"];
Here is the object I want to bind data to.这是我要绑定数据的 object。
export class Patient {
dose: number = 20;
mass: number = 30;
gender: string = "male";
age: number = 10;}
Any suggestions on what's wrong?关于有什么问题的任何建议?
There is nothing wrong on your HTML:您的 HTML 没有任何问题:
<md-input-container class="home__input-container">
<md-select placeholder="sex | male/female" [(ngModel)]="patient.gender" name="gender">
<md-option *ngFor="let gender of genders" [value]="gender">
{{gender}}
</md-option>
</md-select>
</md-input-container>
On your component.ts, you should be using an object, instead of a class for patient.在您的 component.ts 上,您应该为患者使用 object,而不是 class。 However, you may use an interface (Patient) to define the typings for your
patient
object.但是,您可以使用接口(患者)为
patient
object 定义分型。
interface Patient {
dose: number;
mass: number;
gender: string;
age: number;
}
And within your class itself,在您的 class 本身内,
genders: string[] = ["male","female"];
patient: Patient = {
dose: 20,
mass: 30,
gender: 'male',
age: 10,
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.