Angular动态表单使用
2019-08-08 17:01
351 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/moqiuqin/article/details/98874932
使用动态表单目的就是不用去手动编写太多的HTML代码,设置好之后就由数据的多少以及类型来动态生成表单,提高效率。在Angular的官网中介绍动态表单时,夹杂了一些业务方面的东西,增加了不熟悉的人学习的难度,这里直接就是为了演示怎么产生和使用动态表单。
使用动态表单时,有可能需要input类型、textarea类型或者其他表单类型等,在拿到数据构建相关控件时,需要制定一些基本的东西,比如控件类型、值等。
首先创建一个基类,这个看自己的需要,要不要基类都是自己选择,这里直接拿官网的一些配置
//基类 export class BaseControl<T>{ value: T; key: string; label: string; required: boolean; order: number; controlType: string; placeholder: string; constructor(options: { value?: T, key?: string, label?: string, required?: boolean, order?: number, controlType?: string, placeholder?: string } = {}) { this.value = options.value; this.key = options.key || ''; this.label = options.label || ''; this.required = !!options.required; this.order = options.order === undefined ? 1 : options.order; this.controlType = options.controlType || ''; this.placeholder = options.placeholder || ''; } }
创建子控件类
//textarea类 export class TextArea extends BaseControl<string>{ controlType = "textarea"; rows: number; constructor(options: {} = {}) { super(options); this.rows = options['rows'] || 1; } }
//input类,默认文本类型 export class TextBox extends BaseControl<string>{ controlType = "textbox"; type: string; constructor(options: {} = {}) { super(options); this.type = options["type"] || ""; } }
然后构建控件
export class DynamicFromComponent implements OnInit { constructor() { } form:FormGroup; controls:BaseControl<any>[]; ngOnInit() { this.controls=this.createControls(); this.form=this.toFromGroup(this.controls); } //构建控件,如果需要放一些默认数据以及其他的验证都在这里进行 createControls() { return [ new TextArea({ placeholder: "地方描述,最多120字。", rows: 3, key:"desc1" }), new TextArea({ placeholder: "路线描述,最多120字。", rows: 3, key:"desc2" }), new TextBox({ placeholder: "用户名", value:"张三", key:"userName" }) ] } //循环控件加到formgroup上 toFromGroup(controls:BaseControl<any>[]){ let group: any = {}; controls.forEach(item => { //这个是要绑到formControlName上的值,如果都不设置具体名称的话, //最后提交拿到只有最后改变的那个控件的值,我这写的和官网上的不一样 //我这里是一种投机行为 group[item.key] = new FormControl(item.key || ""); }); return new FormGroup(group); } onSubmit(){ //这里是提交时获取到的所有表单数据,如果都不填获取的是默认的值 console.log(this.form.value); } }
前台使用
<form (ngSubmit)="onSubmit()" [formGroup]="form"> <div *ngFor="let item of controls"> <div [ngSwitch]="item.controlType"> <!--通过判断类型来显示哪种表单--> <input *ngSwitchCase="'textbox'" [type]="item.type" value="{{item.value}}" [formControlName]="item.key" placeholder="{{item.placeholder}}"> <textarea *ngSwitchCase="'textarea'" value="{{item.value}}" rows="{{item.rows}}" [formControlName]="item.key" placeholder="{{item.placeholder}}"></textarea> </div> </div> <button type="submit">保存</button> </form>
结果
相关文章推荐
- Angular动态表单生成(四)
- Django:我是怎么做到使用django动态定义表单(form)的
- Angular动态表单生成(八)
- 使用JQuery 表单动态添加和删除行
- AngularJS使用angular-formly进行表单验证
- angular中表单的使用
- Angular动态表单生成(七)
- 使用Angular提交表单
- ajax无法正常下载poi动态生成word文件,可使用form表单提交解决问题
- angular 1.2.29版本下 动态添加多个表单、 校验全部、 提交 、ng-form方案
- angular自定义指令使用$compile动态生成html
- jQuery使用动态渲染表单功能完成ajax文件下载
- 使用JavaScript实现动态创建含合并单元格(行)的表单
- Angular 使用tabset 动态加载组件参数注入
- Angular使用ng-messages与PHP进行表单数据验证
- 使用jQuery制作高级炫酷吊炸天表单,页面加载完成文本框获得焦点,文本框设置默认文本,文本字数动态显示.....
- jQuery使用动态渲染表单功能完成ajax文件下载
- 使用Jquery动态加入对象的集合属性,提交集合属性到表单
- jQuery使用动态渲染表单功能完成ajax文件下载
- 使用zTree实现导航树、angular实现数据动态绑定功能