码迷,mamicode.com
首页 > 其他好文 > 详细

[Angular2 Form] Create and Submit an Angular 2 Form using ngForm

时间:2016-09-27 19:34:24      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:

Forms in Angular 2 are essentially wrappers around inputs that group the input values together into an object and also check that all the inputs are valid. Angular 2 ‘sngForm allows you to get a reference to that object and validity and use them to display information about the form or use the ngSubmit event to save information from the form.

 

Make sure you need to add ‘name‘ prop to both form and input fields. This helps to structure the form model.

  <form action="" name="myForm" #formRef="ngForm" (ngSubmit)="onSubmit(formRef.value)">
    Firstname: <input type="text" name="firstName"ngModel required>
    <button [disabled]="!formRef.valid">Submit</button>
  </form>
  <pre>
    form value: {{formRef.value | json}}
    form valid: {{formRef.valid | json}}
  </pre>

 

ngModel is reuqire by ngForm, so you need to use ngModel on input field even you don‘t assign anything to it.

[Angular2 Form] Create and Submit an Angular 2 Form using ngForm

标签:

原文地址:http://www.cnblogs.com/Answer1215/p/5913880.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!