zoukankan      html  css  js  c++  java
  • angular 高级表单拆分使用


    我们在写复杂结构的表单的时候, 想的是怎么可以更好的偷懒且代码结构简化易于维护

    表单传给子代执行

    <app-time-module-four  [formGroup]="validateForm"></app-time-module-four>
    
      validateForm!: FormGroup;
    ngOnInit(): void {
        this.validateForm = this.fb.group({
          indicatorArr: [''],
        });
      }
    

    <input  class="form-control"  [formControl]="formKey">
        
    @Component({
      viewProviders: [{provide: ControlContainer, useExisting: FormGroupDirective}]
    })
    export class TimeModuleFourComponent implements OnInit {
      formKey!: FormControl;
    
      constructor(private fg: FormGroupDirective) {
      }
    
      ngOnInit(): void {
        this.formKey = (this.fg.form.get('indicatorArr') as FormControl);
      }
    
    }
    
    

    https://stackblitz.com/edit/angular-ssryuc?file=src%2Fapp%2Fapp.component.html

    思考复杂结构的实现

    <form nz-form [formGroup]="validateForm">
      <app-time-module-four formGroupName="add"></app-time-module-four>
    </form>
    
    this.validateForm = this.fb.group({
          indicatorArr: [''],
          add: this.fb.group({
            sex: ['',[Validators.required]]
          })
        });
    

      viewProviders: [{provide: ControlContainer, useExisting: FormGroupName}]
    })
    export class TimeModuleFourComponent implements OnInit {
      formGroupOne!: FormGroup;
    
      constructor(private fg: FormGroupName) {
      }
    
      ngOnInit(): void {
        this.formGroupOne = this.fg.control;
      }
    }
    
    

    子html

    <form nz-form [nzLayout]="'inline'" [formGroup]="formGroupOne">
      <nz-form-item>
        <nz-form-label [nzSpan]="5">Fail</nz-form-label>
        <nz-form-control
          [nzSpan]="12"
          nzErrorTip="Should be combination of numbers & alphabets"
        >
          <input nz-input formControlName="sex" name="errorValid" />
        </nz-form-control>
      </nz-form-item>
    </form>
    

    上一个完整复杂的情况

    export class RestrainComponent implements OnInit, OnDestroy {
          validateForm!: FormGroup;
          constructor(private fb: FormBuilder) {}
    	  ngOnInit(): void {
                  this.validateForm = this.fb.group({
                     restrainArr: this.fb.array([])
                   });
                  this.restrainArr.push(this.restrainListOne);
          }
      get restrainListOne(): FormGroup {
        return this.fb.group({
          name: ['', [Validators.required, Validators.maxLength(100)]],
          inhibitionSort: [''],
          status: [false],
          incidentInhibitionGroupBeanList: this.fb.array([]),
          // 加个展开收起字段
          unfold: [false]
        });
      }
        
      get restrainArr(): FormArray {
        return this.validateForm.get('restrainArr') as FormArray;
      }
    }
    

    html

      <form nz-form [formGroup]="validateForm">
        <div class="yl-content-box" formArrayName="restrainArr" *ngFor="let item of restrainArr.controls;let index=index;">
            <ty-restrain-list *ngIf="item.get('unfold').value" class="yl-content-particulars"
                              [formGroup]="item"></ty-restrain-list>
          </ng-container>
        </div>
      </form>
    

    ({
        ...
     viewProviders: [{provide: ControlContainer, useExisting: FormGroupDirective}]
    })
    export class RestrainListComponent implements OnInit {
      fbForm: FormGroup;
    
      constructor(private fg: FormGroupDirective) {
      }
    
      ngOnInit(): void {
        this.fbForm = this.fg.form;
      }
    
    }
    
    <form nz-form [formGroup]="fbForm">
      <nz-form-item class="yl-item">
        <nz-form-label nzRequired [nzSpan]="2" class="yl-item-label">策约名称</nz-form-label>
        <nz-form-control [nzErrorTip]="templateError1" [nzSpan]="10">
          <input type="text" nz-input formControlName="name" placeholder="请输入策略名称" class="yl-item-input"/>
        </nz-form-control>
      </nz-form-item>
    </form>
    

    管道更新

    https://stackoverflow.com/questions/43799656/manually-trigger-pipe-update
    就是更新的时候传入一个参数num=1
    更改的时候 ++num 让他递增,更新管道
    

    决定自己的高度的是你的态度,而不是你的才能

    记得我们是终身初学者和学习者

    总有一天我也能成为大佬

  • 相关阅读:
    微信小程序里使用 Redux 状态管理
    ES6基础
    微信小程序入门
    Redis 安装
    ServiceStack.Redis 使用
    改善C#程序,提高程序运行效率的50种方法
    Jquery Ajax调用aspx页面方法
    WebAPI创建
    Find the Difference -- LeetCode
    Encode and Decode Strings -- LeetCode
  • 原文地址:https://www.cnblogs.com/fangdongdemao/p/15443062.html
Copyright © 2011-2022 走看看