【问题标题】:Is there any option to order form.value in a custom way in Angular?是否有任何选项可以在 Angular 中以自定义方式订购 form.value?
【发布时间】:2021-03-04 23:16:14
【问题描述】:

我正在 Angular 中练习模板驱动的表单。下面是这个简单表格的例子。

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>

登录到控制台的默认方式如下,

密码:“123456” 用户名:“shah”

但我希望按键应该按照表格中的顺序显示,像这样,

用户名:“shah”密码:“123456”

有什么办法吗?

【问题讨论】:

标签: angular


【解决方案1】:

Javascript 中的对象没有顺序。

您可以使用内部具有排序功能的 FormArray。

或者如果你真的想用对象来做,你可能必须将你的控件存储在一个数组中并更改顺序

【讨论】:

    猜你喜欢
    • 2016-12-23
    • 2020-04-12
    • 1970-01-01
    • 2019-08-29
    • 2019-02-27
    • 1970-01-01
    • 2021-02-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多