简体   繁体   English

Angular FormControl对象能够接受任何类型的输入值,还是仅限于typescript原始类型?

[英]Angular FormControl objects are able to accept input values of any type, or are restricted to typescript primitive types?

I am using FormControl objects in a FormGroup to create a reactive form in Angular. 我在FormGroup中使用FormControl对象在Angular中创建一个被动形式。 There is no problem when I pass primitive arguments as a value of an HTML input select control. 将原始参数作为HTML输入选择控件的值传递时没有问题。 However, when I pass an object of a self-defined class, the value in the FormControl is reduced to [object Object]. 但是,当我传递自定义类的对象时,FormControl中的值将减少为[object Object]。

The system I am working in includes: Angular CLI: 7.1.4; 我正在使用的系统包括:Angular CLI:7.1.4; Node: 10.15.0; 节点:10.15.0; Angular: 7.1.4; Angular:7.1.4; rxjs 6.3.3; rxjs 6.3.3; typescript 3.1.6; 打字稿3.1.6; webpack 4.23.1; webpack 4.23.1; Linux rdz1 4.15.0-43-generic #46-Ubuntu SMP Thu Dec 6 14:45:28 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux Linux rdz1 4.15.0-43-generic#46-Ubuntu SMP Thu Dec 6 14:45:28 UTC 2018 x86_64 x86_64 x86_64 GNU / Linux

import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl } from '@angular/forms';

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

export class HeaderNotesComponent implements OnInit {
    ndcForm = new FormGroup({
        noteType: new FormControl(''),
        noteDate: new FormControl(''),
        noteFor: new FormControl(''),
        noteTo: new FormControl(''),
        noteDetail: new FormControl(''),
        noteDetailVal: new FormControl(''),
        noteChkRefInvoice: new FormControl('')
    });

    ngOnInit() { this.onChanges(); }

    onChanges(): void{
        this.ndcForm.valueChanges
        .subscribe( val => console.log(val))
    }
}

The console shows something like: {noteType: "credit", noteDate: "2019-01-01", noteTo: [object Object], ... } 控制台显示如下内容:{noteType:“credit”,noteDate:“2019-01-01”,noteTo:[object Object],...}

I am passing an object {param1: val1, parm2: val2} to "noteTo" , so I would expect to watch this value in the console, however it is showing [object Object]. 我将一个对象{param1:val1,parm2:val2}传递给“noteTo”,所以我希望在控制台中看到这个值,但它显示的是[object Object]。 It looks like if the object has been stringified. 看起来好像是对象已经被字符串化了。

I have found the answer. 我找到了答案。 In the form, instead of using: 在表单中,而不是使用:

<option *ngFor="let cargoAg of dfCargoAgs" [value]="cargoAg">{{cargoAg.nombre}}</option>

I had to use: 我不得不使用:

<option *ngFor="let cargoAg of dfCargoAgs" [ngValue]="cargoAg">{{cargoAg.nombre}}</option>

So [value] accepts only primitive types, but [ngValue] can accept objects of any class. 所以[value]只接受原始类型,但[ngValue]可以接受任何类的对象。 I hope this can be helpful. 我希望这会有所帮助。

You are correct in assuming that the object is being "stringified". 假设对象正在“字符串化”,你是正确的。

You can override toString() on your custom type in order to get the desired output in the form in HTML. 您可以在自定义类型上覆盖toString() ,以便在HTML中以表单形式获得所需的输出。

However, if you want to be able to convert back from the form to get the value as an object of that custom type then you will have to create a method for doing so, which can be kind of annoying. 但是,如果您希望能够从表单转换回来获取该值作为该自定义类型的对象,那么您将不得不创建一个方法来执行此操作,这可能有点烦人。

Another solution would be to break out the properties of your custom type into a nested FormGroup inside of your ndcForm FormGroup , like so: 另一种解决办法是打破了你的自定义类型的属性到嵌套FormGroup您的内部ndcForm FormGroup ,就像这样:

ndcForm: FormGroup = new FormGroup({
    noteType: new FormControl(''),
    noteDate: new FormControl(''),
    noteFor: new FormControl(''),
    noteTo: new FormGroup({
        any: new FormControl(''),
        custom: new FormControl(''),
        property: new FormControl('')
    }),
    noteDetail: new FormControl(''),
    noteDetailVal: new FormControl(''),
    noteChkRefInvoice: new FormControl('')
});

You will have to take some care to extract each value from the object to place it into the form, and vice versa. 您需要注意从对象中提取每个值以将其放入表单中,反之亦然。

Go to the Angular docs for info on nesting FormGroup instances . 有关嵌套FormGroup实例的信息,请转到Angular文档。

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

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