简体   繁体   English

如何通过重新输入原始值使 Angular 表单字段“干净”?

[英]How to make an Angular form field "clean" by re-typing original value?

In my code base using an Angular form, the user opens an entry to be edited and the various fields are pre-filled with the current values.在我使用 Angular 表单的代码库中,用户打开一个要编辑的条目,并且各个字段都预先填充了当前值。 The Save button starts disabled, because nothing is dirty in an Angular sense. Save按钮开始被禁用,因为从 Angular 的角度来看,没有什么是dirty的。 If the user enters a field and starts typing, the field becomes dirty and the Save becomes enabled.如果用户输入一个字段并开始打字,该字段会变脏并且启用保存

During the course of editing the form values, there are checks that disable the Save button if:在编辑表单值的过程中,有一些检查会在以下情况下禁用“保存”按钮:

  • required fields are empty,必填字段为空,
  • fields requiring a certain format have invalid entries,需要特定格式的字段包含无效条目,
  • entries are longer than permitted,条目超过允许的长度,

... and the like, and finally ......等等,最后

  • the value, even if dirty , is now back to the original value when the user opened the form.当用户打开表单时,即使dirty的值现在也恢复到原始值。

Here is the essence of it:这是它的本质:

<!-- template code -->
<input formControlName="name" type="text" (keyup)="handleNameChange()">
. . .
<button [disabled]="!updateForm.valid || disableSave">Save</button>
. . .

// backing code
public handleNameChange(): void {
  this.disableSave = this.updateForm.controls.name.value === this.original.name;
}

Two questions:两个问题:

A. Though it seems "obvious" from one perspective (nothing has changed so why should one have to save it?), is the last point a common, best practice in the UX sense? A. 虽然从一个角度来看这似乎是“显而易见的”(什么都没有改变,所以为什么要保存它?),最后一点是 UX 意义上的常见最佳实践吗? Would love to see articles or references about it.希望看到有关它的文章或参考资料。

B. Is there a way for an Angular form to do this (ie set a field back to non-dirty) automatically without my having to add explicit code to check the new value against the old value? B. 有没有办法让 Angular 表单自动执行此操作(即,将字段设置回非脏),而无需添加显式代码来检查新值与旧值?

What you're wanting to do makes sense for a UI, although you might consider allowing the user to hit the button and display some sort of message alerting them that no changes had actually been made.您想要做的对于 UI 来说是有意义的,尽管您可能会考虑允许用户点击按钮并显示某种消息,提醒他们实际上没有进行任何更改。 Or if it's disabled, tell them why.或者如果它被禁用,告诉他们为什么。 Question A could probably be expanded on more over at UX StackExchange ( Relevant question over there )问题 A 可能会在UX StackExchange上进一步扩展( 相关问题在那里

As far as why Angular chooses to have the "dirty" state work as it does it more of a choice by them I supposed as the dirty state is triggered by any "change" event.至于为什么 Angular 选择让“脏”状态工作,因为它更多是他们的选择,我认为脏状态是由任何“更改”事件触发的。 It doesn't keep track of the original value as you already saw.它不会像您已经看到的那样跟踪原始值。

A control is dirty if the user has changed the value in the UI.如果用户更改了 UI 中的值,则控件是脏的。 ( Source ) 来源

There's also information in the HTML5 input spec that talks about dirty fields HTML5 输入规范中还有关于脏字段的信息

Each input element has a boolean dirty value flag.每个输入元素都有一个布尔脏值标志。 The dirty value flag must be initially set to false when the element is created, and must be set to true whenever the user interacts with the control in a way that changes the value.在创建元素时,脏值标志必须最初设置为 false,并且必须在用户以更改值的方式与控件交互时设置为 true。

This doesn't say anything about changing the value back to false/clean.这并没有说明将值改false/clean。 So Angular might be following that.所以 Angular 可能会遵循这一点。

Unfortunately the answer to question B is no.不幸的是,问题 B 的答案是否定的。 You'll have to compare the values in some way and then use markAsPristine() or reset the form to do that or just have a separate manual check apart from using the "dirty" state.您必须以某种方式比较这些值,然后使用 markAsPristine() 或重置表单来执行此操作,或者除了使用“脏”状态外,只需进行单独的手动检查。

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

相关问题 在键入时获取Angular 4中输入字段的值 - Getting value of the input field in Angular 4 while typing Angular 2-如何清理表单中的数组 - Angular 2 - how to clean array in form 如何在Angular 6+中使mat-form-field-underline 0的高度 - How to make the height of the mat-form-field-underline 0 in Angular 6+ 如何在angular2中使反应形式的字段处于只读状态 - How to make the field in reactive form to be in read only state in angular2 有效值为角度7格式字段 - Is valid value angular 7 form field Angular 9 将表单重置为原始 state 其中一个字段是一个数组 - Angular 9 Reset form to original state where one field is an array 如何根据使用 Angular rective 表单选择到另一个字段中的值来设置表单字段的值? - How can I set the value of a form field basing on the value selected into another field using Angular rective form? 如何在 angular 中制作自定义验证器以检查基于其他表单字段的表单字段? - how to make a custom validator in angular to check a form field based on the other form field? 如何在Angular表单字段的select选项中显示默认值 - How to display default value in select option in Angular form field Angular2 Karma测试 - 如何使表单字段“脏”? - Angular2 Karma Test - How to make a form field “dirty”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM