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

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.

时间: 2024-12-21 15:34:00

[Angular2 Form] Create and Submit an Angular 2 Form using ngForm的相关文章

[Angular2 Form] Create Radio Buttons for Angular 2 Forms

Using Radio Buttons in Angular 2 requires a basic understanding of forms as well as how their labels will match up with each input. This lesson shows how to use *ngFor with radio buttons and covers the quirks of the id property and forattributes as w

angular reactive form

这篇文章讲了angular reactive form, 这里是angular file upload 组件 https://malcoded.com/posts/angular-file-upload-component-with-express/ 原文:https://malcoded.com/posts/angular-fundamentals-reactive-forms/ ------------------------------------ Forms are arguably o

jquery中form中使用submit出现的问题,未解决

$("#login_btn").click(function(){在type为submit的按钮下 if($("#id_password").val().length == 0){//检验密码是否为空 $('form').submit(function(){ alert("1") return false; }); }else if($("#vali").val() != code){//验证码是否正确 $('form').s

asp.net中通过form表单submit提交到后台的实例

前台<body>中的代码: <body> <div id="top"> </div> <form id="login" name="login" action="?Action=Login" method="post"> <div id="center"> <div id="center_left&q

Django---FORM组件.FORM组件的字段,FORM组件校验流程,FORM组件的全局和局部钩子,FORM和Model的组合

Django---FORM组件.FORM组件的字段,FORM组件校验流程,FORM组件的全局和局部钩子,FORM和Model的组合 一丶FORM的介绍 1.生成页面可用的HTML标签 2.对用户提交的数据进行校验 3.保留上次输入内容 二丶使用form组件实现注册功能 from django import forms # 导入forms组件 # 按照Django form组件的要求自己写一个类 class RegForm(forms.Form): # 继承Form name = forms.Ch

sharepoint 2013基于AD的Form表单登录(二)——form登录页面自定义

配置好了sharepoint 2013基于AD的Form登录,只是成功了第一步,如何自定义登录页呢?特别是不要出现sharepoint2013自带登录页面,每次登录前还需要选择是否是form或者windows验证. 打开vs2012新建sharepoint 2013 project,在layouts目录下添加application page,页面命名为CustomLogin.aspx. 前台页面:为避免母版页中其他控件影响,注意继承的是simple.master         后台页面:继承F

[Angular2 Form] Create custom form component using Control Value Accessor

//switch-control component import { Component } from '@angular/core'; import { ControlValueAccessor, NG_VALUE_ACCESSOR, NG_VALIDATORS, Validators} from '@angular/forms'; @Component({ selector: 'switch-control', templateUrl: './switch-control.componen

[Angular2 Form] Build Select Dropdowns for Angular 2 Forms

Select Dropdowns in Angular 2 a built with select and option elements. You use *ngFor to loop through your values and create options and use ngModel to keep track of the value as it changes. <form #formRef="ngForm"> <select name="l

[Angular2 Form] Use RxJS Streams with Angular 2 Forms

Angular 2 forms provide RxJS streams for you to work with the data and validity as it flows out of the forms. These streams allow you handle complex scenarios and asynchronous scenarios with relative ease. This example shows you how to log out the va