首页 > 代码库 > [Angular2 Form] Create and Submit an Angular 2 Form using ngForm
[Angular2 Form] Create and Submit an Angular 2 Form using ngForm
Forms in Angular 2 are essentially wrappers around input
s 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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。