博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
@angular/cli项目构建--Dynamic.Form
阅读量:7070 次
发布时间:2019-06-28

本文共 5173 字,大约阅读时间需要 17 分钟。

导入所需模块:

ReactiveFormsModule

DynamicFormComponent.html

{
{formItem.label}} is required

DynamicFormComponent.ts

import {Component, Input, OnInit} from '@angular/core';import {FormItemBase} from './form-item-base';import {FormGroup} from '@angular/forms';@Component({  selector: 'app-dynamic-form',  templateUrl: './dynamic-form.component.html',  styleUrls: ['./dynamic-form.component.css']})export class DynamicFormComponent implements OnInit {  @Input() formItem: FormItemBase
; @Input() form: FormGroup; constructor() { } ngOnInit() { } get isValid() { return this.form.controls[this.formItem.key].valid; }}

FormItemBase.ts

export class FormItemBase
{ value: T; key: string; label: string; required: boolean; order: number; controlType: string; constructor(options: { value?: T, key?: string, label?: string, required?: boolean, order?: number, controlType?: 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 || ''; }}

FormTextbox.ts

import {FormItemBase} from './form-item-base';export class FormTextbox extends FormItemBase
{ controlType = 'textbox'; type: string; constructor(options: {} = {}) { super(options); this.type = options['type'] || ''; }}

FormDropdown.ts

import {FormItemBase} from './form-item-base';export class FormDropdown extends FormItemBase
{ controlType = 'dropdown'; options: {key: string, value: string}[] = []; constructor(options: {} = {}) { super(options); this.options = options['options'] || []; }}

FormItemControl.ts

import {Injectable} from '@angular/core';import {FormItemBase} from './form-item-base';import {FormControl, FormGroup, Validators} from '@angular/forms';@Injectable()export class FormItemControlService {  constructor() {  }  toFormGroup(formItems: FormItemBase
[]) { const group: any = {}; formItems.forEach(formItem => { group[formItem.key] = formItem.required ? new FormControl(formItem.value || '', Validators.required) : new FormControl(formItem.value || ''); }); return new FormGroup(group); }}

QuestionComponent.html

QuestionComponent.ts

import { Component, OnInit } from '@angular/core';import {QuestionFromService} from './question-form/question-form.service';@Component({  selector: 'app-question',  templateUrl: './question.component.html',  styleUrls: ['./question.component.css']})export class QuestionComponent implements OnInit {  questions: any[];  constructor(questionFormService: QuestionFromService) {    this.questions = questionFormService.getQuestionFormItems();  }  ngOnInit() {  }}

QuestionFormComponent.html

Saved the following values
{
{payLoad}}

QuestionFormComponent.ts

import {Component, Input, OnInit} from '@angular/core';import {FormGroup} from '@angular/forms';import {FormItemBase} from '../../common/component/dynamic-form/form-item-base';import {FormItemControlService} from '../../common/component/dynamic-form/form-item-control.service';@Component({  selector: 'app-question-form',  templateUrl: './question-form.component.html',  styleUrls: ['./question-form.component.css']})export class QuestionFormComponent implements OnInit {  form: FormGroup;  payLoad = '';  @Input()  questions: FormItemBase
[] = []; constructor(private fromItemControlService: FormItemControlService) { } ngOnInit() { this.form = this.fromItemControlService.toFormGroup(this.questions); } onSubmit() { this.payLoad = JSON.stringify(this.form.value); }}

 QuestionForm.service.ts

import {Injectable} from '@angular/core';import {FormItemBase} from '../../common/component/dynamic-form/form-item-base';import {FormDropdown} from '../../common/component/dynamic-form/form-dropdown';import {FormTextbox} from '../../common/component/dynamic-form/form-textbox';@Injectable()export class QuestionFromService {  getQuestionFormItems() {    const questionFormItems: FormItemBase
[] = [ new FormDropdown({ key: 'brave', label: 'Bravery Rating', options: [ {key: 'solid', value: 'Solid'}, {key: 'great', value: 'Great'}, {key: 'good', value: 'Good'}, {key: 'unproven', value: 'Unproven'} ], order: 3 }), new FormTextbox({ key: 'firstName', label: 'First name', value: 'Bombasto', required: true, order: 1 }), new FormTextbox({ key: 'emailAddress', label: 'Email', type: 'email', required: false, order: 2 }) ]; return questionFormItems.sort((a, b) => a.order - b.order); }}

 

转载于:https://www.cnblogs.com/Nyan-Workflow-FC/p/8044694.html

你可能感兴趣的文章
Spring Security身份认证之HelloSpringSecurity(附源码)
查看>>
WPF实例秀——不用属性也Binding
查看>>
打造Ubuntu下的SLAMP
查看>>
SoapUI实践:自动化测试、压力测试、持续集成
查看>>
Redis中Value使用hash类型的效率是普通String的两倍
查看>>
爪哇国新游记之八----读写文件及数组排序
查看>>
[Android]在Dagger 2中使用RxJava来进行异步注入(翻译)
查看>>
是技术还是态度,网易的视频Title
查看>>
ES 處於“initializing”狀態,此時主節點正在嘗試將分片分配到集群中的數據節點。 如果您看到分片仍處於初始化或未分配狀態太長時間,則可能是您的集群不穩定的警告信號。...
查看>>
切换RequiredFieldValidator和RegularExpressionValidator提示信息的控件
查看>>
基于类的封装[基础]
查看>>
android studio插件提升工作效率
查看>>
What is VMR(Video Mixing Render)-From MSDN
查看>>
Linux下安装nmap扫描工具
查看>>
git 创建branch分支【转】
查看>>
北京某公司.NET面试题
查看>>
解决异常“SqlParameterCollection 只接受非空的 SqlParameter 类型对象。”
查看>>
PostgreSQL通过mysql_fdw访问MySQL数据库
查看>>
REST风格的原则
查看>>
Struts分页的一个实现
查看>>