Angular学习系列三:表单数据读取

1:使用vscode打开事先准备好的空项目

2:创建表单组件:ng g component components/form

3:在app.component.html中引用该标签:<app-form></app-form>

4:引用表单组件,才能实现双向绑定,在app.module.ts里面:

import {FormsModule} from ‘@angular/forms‘; 并在imports中配置组件:FormsModule

5:表单数据绑定:文本框,单选框,复选框,文本域 数据读取

界面(form.component.html)代码:

 1 <p>form works!</p>
 2 <div>
 3     <table>
 4         <tr>
 5             <td>name</td>
 6             <td>
 7                 <input id=‘name‘ [(ngModel)]="peopleInfo.name" />
 8             </td>
 9         </tr>
10         <tr>
11             <td>gender:</td>
12             <td>
13                 <input type="radio" value=‘male‘ [(ngModel)]="peopleInfo.gender" id="gender1" name="gender">
14                 <label for=‘gender1‘>男</label>
15
16                 <input type="radio" value=‘female‘ [(ngModel)]="peopleInfo.gender" id="gender2" name="gender">
17                 <label for=‘gender2‘>女</label>
18             </td>
19         </tr>
20         <tr>
21             <td>city</td>
22             <td>
23                 <select name=‘city‘ id=‘city‘ [(ngModel)]="peopleInfo.city">
24                     <option [value]=‘item‘ *ngFor="let item of peopleInfo.cities">{{item}}</option>
25                 </select>
26             </td>
27         </tr>
28         <tr>
29             <td>爱好</td>
30             <td>
31                 <span *ngFor="let item of peopleInfo.hobby;let key=index">
32                     <input type="checkbox" [id]="‘check‘+key" [(ngModel)]="item.checked"><label
33                         [for]="‘check‘+key">{{item.title}}</label>
34                 </span>
35             </td>
36         </tr>
37         <tr>
38             <td>
39                 备注:
40             </td>
41             <td>
42                 <textarea name=‘mark‘ cols=30 rows=‘10‘ [(ngModel)]="peopleInfo.mark"></textarea>
43             </td>
44         </tr>
45         <tr>
46             <td><button (click)="doSubmit()">提交</button></td>
47             <td></td>
48         </tr>
49         <tr>
50             <td>
51                 结果输出:
52             </td>
53             <td>
54                 <pre>
55                 {{peopleInfo | json}}
56             </pre>
57             </td>
58         </tr>
59     </table>
60 </div>

组件(form.component.ts)代码:

 1 import { Component, OnInit } from ‘@angular/core‘;
 2
 3 @Component({
 4   selector: ‘app-form‘,
 5   templateUrl: ‘./form.component.html‘,
 6   styleUrls: [‘./form.component.css‘]
 7 })
 8 export class FormComponent implements OnInit {
 9   public peopleInfo: any = {
10     name: ‘默认name‘,
11     gender: "female",
12     cities: [
13       ‘beijing‘,
14       ‘shanghai‘,
15       ‘guangzhou‘
16
17     ],
18     city:‘‘,
19     hobby:[
20       {title:"逛街",checked:false},
21       {title:"读书",checked:false},
22       {title:"打球",checked:false},
23       {title:"旅游",checked:false},
24       {title:"健身",checked:false},
25     ],
26     mark:"",
27
28   };
29   constructor() { }
30
31   ngOnInit() {
32   }
33
34   doSubmit() {
35     console.log(this.peopleInfo);
36   }
37 }

界面效果:

原文地址:https://www.cnblogs.com/hanliping/p/12150547.html

时间: 2024-07-31 13:26:36

Angular学习系列三:表单数据读取的相关文章

angular学习笔记(三十)-指令(5)-link

这篇主要介绍angular指令中的link属性: link:function(scope,iEle,iAttrs,ctrl,linker){ .... } link属性值为一个函数,这个函数有五个参数:scope,iEle,iAttrs,ctrl,linker scope:指令所在的作用域,这个scope和指令定义的scope是一致的.至于指令的scope,会在讲解scope属性的时候详细解释 iEle:指令元素的jqLite封装.(也就是说iEle可以调用angular封装的简版jq的方法和属

angular学习笔记(三十)-指令(6)-transclude()方法(又称linker()方法)-模拟ng-repeat指令

在angular学习笔记(三十)-指令(4)-transclude文章的末尾提到了,如果在指令中需要反复使用被嵌套的那一坨,需要使用transclude()方法. 在angular学习笔记(三十)-指令(5)-link文章也提到了link函数的第五个参数linker. 这篇文章就来讲解一下transclude()方法(linker()方法),是怎么使用的,另外,它也是compile函数的第三个参数,用法一样. 下面就通过自己写一个简易的模拟ngRepeat的指令cbRepeat,来了解linke

angular学习笔记(三十)-指令(7)-compile和link(2)

继续上一篇:angular学习笔记(三十)-指令(7)-compile和link(1) 上一篇讲了compile函数的基本概念,接下来详细讲解compile和link的执行顺序. 看一段三个指令嵌套的代码: html: <body> <div ng-controller="compileCtrl"> <level-one> <level-two> <level-three> hello,{{name}} </level-

angular学习笔记(三十)-指令(2)

本篇主要讲解指令中的 restrict属性, replace属性, template属性 这三个属性 一. restrict: 字符串.定义指令在视图中的使用方式,一共有四种使用方式: 1. 元素: E 2. 属性: A 3. 样式类: C 4. 注释: M restrict的值可以是上面四个字母的任意一个或多个的组合. 不指定的话默认为A. 二. replace: 布尔值.是否将指令元素替换,可以有两个值: 1.true: 替换整个使用指令的元素 2.false: 不替换整个使用指令的元素,而

angular学习笔记(三十一)-$location(2)

之前已经介绍了$location服务的基本用法:angular学习笔记(三十一)-$location(1). 这篇是上一篇的进阶,介绍$location的配置,兼容各版本浏览器,等. *注意,这里介绍的是基于angular-1.3.2版本的,低版本的$location可能会有问题. hashbang模式和history api创建单页应用 首先,$location是用在单页应用里的...(废话,angular就是用在单页的)...所以,$location处理的是url改变,但是不刷新页面的情况.

angular学习笔记(三十)-指令(10)-require和controller

本篇介绍指令的最后两个属性,require和controller 当一个指令需要和父元素指令进行通信的时候,它们就会用到这两个属性,什么意思还是要看栗子: html: <outer‐directive> <inner‐directive></inner‐directive> </outer‐directive> 这里有两个指令,一个outer-directive指令元素,它里面又有一个inner-directive指令元素. js: app.directiv

angular学习笔记(三十)-指令(1)

之前在 angular学习笔记(十九)-指令修改dom 里面已经简单的提到了angular中的指令,现在来详细的介绍 '指令' 一.指令的创建: dirAppModule.directive('directive-name',function(){ var obj = { restrict:'string', priority:number, template:'string', templateUrl:'string', replace:bool, transclude:bool or str

SpringMVC学习系列 之 表单标签

http://www.cnblogs.com/liukemng/p/3754211.html 本篇我们来学习Spring MVC表单标签的使用,借助于Spring MVC提供的表单标签可以让我们在视图上展示WebModel中的数据更加轻松. 一.首先我们先做一个简单了例子来对Spring MVC表单表单标签的使用有一个大致的印象,然后再结合例子对各个标签介绍一下如何使用. 1.首先,在com.demo.web.models包中添加一个模型TagsModel内容如下: package com.de

Identity Server4学习系列三

1.简介 在Identity Server4学习系列一和Identity Server4学习系列二之令牌(Token)的概念的基础上,了解了Identity Server4的由来,以及令牌的相关知识,本文开始实战,实现Identity Server4基本的功能. 2.前提 本文基于.Net Core2.1和Indetity Server4 2.3.0,令牌处理包采用IdentityServer4.AccessTokenValidation 2.7.0 3.实战一Identity Server4服