简体   繁体   English

是否有任何选项可以在 Angular 中以自定义方式订购 form.value?

[英]Is there any option to order form.value in a custom way in Angular?

I am practicing template-driven forms in Angular.我正在 Angular 中练习模板驱动的表单。 Below is the example of this simple form.下面是这个简单表格的例子。

 import { NgForm } from "@angular/forms"; onSubmit(form: NgForm): void { console.log(form.value); }
 <form #myForm="ngForm" (ngSubmit)="onSubmit(myForm)"> Username: <input type="text" name="user_name" ngModel/> Password: <input type="password" name="password" ngModel/> <button type="submit">Submit</button> </form>

The default way of logging to the console is like the following,登录到控制台的默认方式如下所示,

password: "123456" user_name: "shah"密码:“123456” 用户名:“shah”

But I want the keys should be shown according to the order in the form, like this,但我希望键应该按照表单中的顺序显示,像这样,

user_name: "shah" password: "123456"用户名:“shah” 密码:“123456”

Is there any way to do this?有没有办法做到这一点?

Object in Javascript doesn't have an order. Javascript 中的对象没有顺序。

You can use a FormArray, which internally has sort functionality.您可以使用内部具有排序功能的 FormArray。

Or if you really wany to do it with object, you probably have to store your controls inside an array and change the order或者如果你真的很想用对象来做,你可能必须将你的控件存储在一个数组中并更改顺序

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

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