Working on angular2 beta Forms
. after alot of searching found nothing useful. hope here somebody help me.
Basically i am bit confused how to use forms in angular2 in a proper manner (i.e using ngControl, ngFormControl etc). i have created one plnkr here
here is my .html code:-
<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>
and .ts code is here:-
CreateGroup: FormBuilder;
constructor(fb: FormBuilder){
console.log('form called');
this.CreateGroup = fb.group({
'name': new Control(),
'password': new Control()
})
}
addNewGroup(value) {
console.log(value);
document.getElementById("myForm").reset();
}
getValue(value){
console.log(value);
}
i am unable to understand how to get values as object from complete form. my form include textboxes,checkboxes,radio and select options. now here are few of my questions.
Q1:- How to get values of radio,checkbox,select using form in angular2. (i dont want to call change
hook for select option as i have used in the plnkr).
Q2:- as in the plnkr after submitting data form control has not been reset. Control of the form remains present but form seems reset. so how to reset the control of forms in angular2.
Q3:- whats the best method to use validation in the forms (if anyone have plnkr showing validation please post it).
i had read this article on forms but still not succesfull with radio checkboxes and select options.
http://blog.ng-book.com/the-ultimate-guide-to-forms-in-angular-2