简体   繁体   English

如何以角度反应形式设置对象键值

[英]How to set the object key value in an angular reactive form

I have an object like this.我有一个这样的对象。

types: {
  2: {
     zoom: true,
     select: true
  },
  4: {
     zoom: true,
     select: true
  },
}

Is it possible to create this json object from a angular form?是否可以从角度形式创建这个 json 对象?

<div formGroupName="types">
    <input type="number" matInput placeholder="Type" [(ngModel)]="searchType" i18n-placeholder>

    <div [ngModelGroup]="searchType">
        <mat-slide-toggle [(ngModel)]="searchType.zoom" color="primary">Zoom</mat-slide-toggle>
        <mat-slide-toggle [(ngModel)]="searchType.select" color="primary">Klik</mat-slide-toggle>
    </div>
</div>

Sadly it's not possible to change the json but the numbers need to be changable.遗憾的是,无法更改 json,但数字需要更改。 The form is reactive driven but I could not find a way so I tried template driven but neither way worked out.表单是被动驱动的,但我找不到方法,所以我尝试了模板驱动,但都没有解决。

Using Angular reactiveForm and Factory to build your desire json object.使用 Angular reactiveFormFactory来构建你想要的json对象。 It is possible to add property dynamically to your object.可以动态地向对象添加property But your props name could not be some numbers as you mentioned in your example;但是您的道具名称不能是您在示例中提到的一些数字; If you want something its better to have an array of objects.如果你想要一些东西,最好有一个对象数组。

So, try something like this:所以,尝试这样的事情:

app.component.ts app.component.ts

import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { TypeFactory } from './type.factory.ts';
import { TypeModel } from './app-type.model';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})

export class AppComponent implements OnInit {
  
  typesForm:FormGroup;
  types: TypeModel[] = [];

  constructor(
    private formBuilder: FormBuilder,
    private typeFactory: TypeFactory,
  ){}

  ngOnInit(){
    this.typesForm = this.formBuilder.group({
      id: ['', [Validators.required]],  
      zoom: [''],
      select: ['']
    });
  }

  regForm = () => {
    let newType = this.typeFactory.createTypeDto(this.typesForm.getRawValues());
    this.types = [ ...types, ...newType];
  }
}

app.component.html应用程序组件.html

<form [formGroup]="typesForm">
  <input formControlName="id" type="number" matInput placeholder="Type" i18n-placeholder>
  <div class="container">
    <mat-slide-toggle formControlName="zoom"  color="primary">Zoom</mat-slide-toggle>
    <mat-slide-toggle formControlName="select"  color="primary">Klik</mat-slide-toggle>
  </div>
  <button class="btn btn-success" (click)="regForm()">Register</button>
</form>

app-type.model.ts app-type.model.ts

export class TypeModel {
  id: number;
  zoom: boolean;
  select: boolean;
}

app.factory.ts应用工厂.ts

import { TypeModel } from './app-type.model';

export class TypeFactory {
  createTypeDto(
    form: any,
  ): TypeModel {
    const model = new TypeModel();
    model.id = form.id;
    model.zoom = form.zoom;
    model.select = form.select;

    return model;
  }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM