致力于angular2 beta Forms.经过大量的搜索后发现没什么用处.希望这里有人帮助我.
基本上我有点困惑如何以适当的方式使用angular2中的表单(即使用ngControl,ngFormControl等).我在这里创建了一个plnkr
这是我的.html代码: -
<form class="form-horizontal" id='myForm' role="form" [ngFormModel]="CreateGroup">
<div class="col-md-7">
Name: <input type="text" id="name" placeholder="Name" class="form-control" ngControl="name">
</div>
<div class="col-md-7">
Password: <input type="text" id="password" placeholder="password" class="form-control" ngControl="password">
</div>
<div class="col-md-7">
<input type="radio" name='type'>Btech
<input type="radio" name='type'>Mtech
</div>
<div class="col-md-7">
<input type="checkbox" >Math
<input type="checkbox">English
<input type="checkbox">Science
</div>
<br>
<div class="col-md-7">
<select #selectOption (change)='getValue(selectOption.value)' class='form-control'>
<option value='1'>One Value</option>
<option value='2'>two Value</option>
<option value='3'>Three Value</option>
</select>
</div>
</form>
<button type="button" (click)="addNewGroup(CreateGroup.value)" class="btn btn-primary btn-lg"> Create </button>
Run Code Online (Sandbox Code Playgroud)
和.ts代码在这里: …
我正在尝试使用Angular2语法从枚举定义创建单选按钮,并将值绑定到具有该枚举类型的属性.
我的HTML包含:
<div class="from_elem">
<label>Motif</label><br>
<div *ngFor="let choice of motifChoices">
<input type="radio" name="motif" [(ngModel)]="choice.value"/>{{choice.motif}}<br>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
在我的@Component中,我声明了一组选择和值:
private motifChoices: any[] = [];
Run Code Online (Sandbox Code Playgroud)
在@Component的构造函数中,我按以下方式填充选项:
constructor( private interService: InterventionService )
{
this.motifChoices =
Object.keys(MotifIntervention).filter( key => isNaN( Number( key )))
.map( key => { return { motif: key, value: false } });
}
Run Code Online (Sandbox Code Playgroud)
单选按钮显示正确,现在我试图将选定的值绑定到属性.但是,当我单击其中一个按钮时,值choice.value将设置为undefined.