angular - 何时应将 FormControl 标记为原始状态?

标签 angular reactive-forms

我讨论了表单应该如何与同事合作,我们对后端驱动的输入字段应该如何响应用户输入有不同的想法。

此讨论基于 Angular's definition of pristine

"pristine" means the user hasn't changed the value since it was displayed in this form.



因此,假设用户有一个自动完成输入来查找附近的机场,当用户选择一个选项时,我们设置 FormControl 的值。到所选选项。

我的理解是,由于用户选择了该选项,因此用户更改了 FormControl。

我的同事的理解是更改是程序化的,而 FormControl基于某些后端进程进行更新,因此该值是“受信任的”并被标记为原始值。

有没有更具体的原始定义?一种考虑后端流程或程序化输入的方法?

最佳答案

我想答案是in the documentationpristine AbstractControl 的属性(property):


  /**
   * A control is `pristine` if the user has not yet changed
   * the value in the UI.
   *
   * @returns True if the user has not yet changed the value in the UI; compare `dirty`.
   * Programmatic changes to a control's value do not mark it dirty.
   */

这里要注意两点:
  • 用户尚未更改 UI 中的值
  • 对控件值的编程更改保持原始状态
  • 关于angular - 何时应将 FormControl 标记为原始状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61599684/

    相关文章:

    Angular 6 Reactive forms - 触发焦点/模糊验证

    Angular 单选按钮与模型不同步

    Angular react 形式 : Debounce only some specific Form Control

    javascript - 当输入为 null 时,ngbtypeahead 函数返回 TypeError

    切换回来后, Angular 动画在 mat-tabs 中不起作用

    Angular Material : mat-select default value when using reactive forms

    angular - PrimeNg ui-float-label 在 react 形式 patchValue() 后不 float

    angular - Swagger 在 Angular 12 中使用 _observableOf(null) 自动生成文件问题?

    angular - Cypress 仍停留在 Chrome stable 80 上

    Angular 默认路由器 socket