[英]Getting the value from a select using nested model in angular 2
I'm trying to learn how to use the Nested Model when creating dynamic forms in my component. 我正在尝试学习在组件中创建动态表单时如何使用嵌套模型 。 However, I'm having trouble with a select tag. 但是,我在选择标签时遇到了麻烦。 I can't seem to set or get the value from it. 我似乎无法设置或从中获取价值。 This can be observed because the default value set in the AppComponent.initPhone() method is setting the dropdown. 可以观察到这是因为在AppComponent.initPhone()方法中设置的默认值正在设置下拉列表。 And when I manually select an item from the dropdown, any item, the value remains undefined (as can be seen in the myForm details section on the page). 而且,当我从下拉列表中手动选择一个项目时,任何一个项目的值都保持未定义状态(如页面上的myForm详细信息部分所示)。
Here's my plunker: http://plnkr.co/edit/iLITdV?p=preview 这是我的朋克: http ://plnkr.co/edit/iLITdV?p=preview
And here's the relevant code (I think) should the plunker ever die: 这是I子曾经死过的相关代码(我认为):
//phone.component.ts
import { Component, Input } from '@angular/core';
import { FormGroup } from '@angular/forms';
@Component({
selector: 'phone',
template: `
<div [formGroup]="phoneForm">
<div class="form-group col-xs-6">
<label>Phone number</label>
<input type="text" class="form-control" placeholder="Phone" formControlName="phone">
<small *ngIf="phoneForm.controls.phone.hasError('required') && phoneForm.controls.phone.touched" class="text-danger">
Phone number is required
</small>
<small *ngIf="(phoneForm.controls.phone.hasError('minlength') || phoneForm.controls.phone.hasError('maxlength')) && phoneForm.controls.phone.touched" class="text-danger">
Phone number is to be 10 numbers long
</small>
</div>
<div class="form-group col-xs-6">
<label>Phone Type</label>
<select class="form-control" formControlName="phoneType">
<option [value]="home" title="For home phone">HOME</option>
<option [value]="sms" title="For text messaging">SMS</option>
<option [value]="tty" title="For the deaf">TTY</option>
</select>
</div>
</div>
`
})
export class PhoneComponent {
@Input('group')
public phoneForm: FormGroup;
}
- -
<!-- snippet from app.component.html -->
<!-- phones -->
<div formArrayName="phones">
<div *ngFor="let phone of myForm.controls.phones.controls; let i = index" class="panel panel-default">
<div class="panel-heading">
<span>Phone {{i+1}}</span>
<span class="glyphicon glyphicon-remove pull-right" *ngIf="myForm.controls.phones.controls.length > 1" (click)="removePhone(i)"></span>
</div>
<div class="panel-body" [formGroupName]="i">
<phone [group]="myForm.controls.phones.controls[i]"></phone>
</div>
</div>
</div>
<div class="margin-20">
<a (click)="addPhone()" style="cursor: default">
Add another phone number +
</a>
</div>
- -
// snippet from app.component.ts
ngOnInit() {
this.myForm = this._fb.group({
name: ['', [Validators.required, Validators.minLength(5)]],
addresses: this._fb.array([]),
phones: this._fb.array([])
});
// add address
this.addAddress();
this.addPhone();
/* subscribe to addresses value changes */
// this.myForm.controls['addresses'].valueChanges.subscribe(x => {
// console.log(x);
// })
}
initAddress() {
return this._fb.group({
street: ['', Validators.required],
postcode: ['']
});
}
initPhone() {
let valids = Validators.compose([
Validators.required,
Validators.minLength(10),
Validators.maxLength(10)
]);
return this._fb.group({
phone: ['502-555-1234', valids],
phoneType: ['sms']
})
}
Instead of using [value]
in your select, use name
: 不要在选择中使用[value]
,而使用name
:
So the following 所以以下
<select class="form-control" formControlName="phoneType">
<option [value]="home" title="For home phone">HOME</option>
<option [value]="sms" title="For text messaging">SMS</option>
<option [value]="tty" title="For the deaf">TTY</option>
</select>
Should be: 应该:
<select class="form-control" formControlName="phoneType">
<option name="home" title="For home phone">HOME</option>
<option name="sms" title="For text messaging">SMS</option>
<option name="tty" title="For the deaf">TTY</option>
</select>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.