简体   繁体   English

验证器如何使用Dart Angular中的变化检测策略“onPush”?

[英]How Validators work with change detection strategy “onPush” in Dart Angular?

My form component has change detection strategy onPush. 我的表单组件有onPush的变化检测策略。 I have a code in ngFor: 我在ngFor中有一个代码:

<div *ngFor="let externalLink of edited.externalLinks">
  <input #xctrl="ngForm" type="text" class="form-control" id="extId" required [(ngModel)]="externalLink.extId" ngControl="linkExtId_{{externalLink.id}}">

  Valid status is {{xctrl.control.valid}}
</div>

User add new row in list by clicking button. 用户通过单击按钮在列表中添加新行。

void addExternalLink() {
  if (edited != null) {
    edited.externalLinks.add(ExternalLink()..id = Uuid().v4().toString());
    _cdr.markForCheck();
  }
}

Input has "required" directive. 输入具有“必需”指令。 After new row appeared "Valid status is true", but input is empty. 新行出现后“有效状态为真”,但输入为空。 If user click on input and exit from control, validation status changed to false. 如果用户单击输入并退出控件,则验证状态将更改为false。 Same effect if add two rows. 如果添加两行,效果相同。 First has status false and second true. First的状态为false,第二个为true。

Why it work like this? 它为什么这样工作? Can you explain behaviors of validations when onPush strategy? 你能解释onPush策略时的验证行为吗?

PS If I change template and delete ngControl="linkExtId_{{externalLink.id}}" then it work as expected. PS如果我更改模板并删除ngControl="linkExtId_{{externalLink.id}}"那么它按预期工作。 On new row I see "Valid status is false". 在新行上,我看到“有效状态为false”。

when you use template variable, it must be unique for each element. 使用模板变量时,每个元素必须是唯一的。 in your code: 在你的代码中:

   <div *ngFor="let externalLink of edited.externalLinks">
        <input #xctrl="ngForm" type="text" class="form-control" id="extId" required 
          [(ngModel)]="externalLink.extId" ngControl="linkExtId_{{externalLink.id}}">

        Valid status is {{xctrl.control.valid}}
   </div>

if you look at it, before you define template variable, you loop on the input element, and all the inputs that create, have the same template variable. 如果你看一下,在定义模板变量之前,你在input元素上循环,所有创建的输入都有相同的模板变量。 because of that you could not validate each input separately. 因此,您无法单独验证每个输入。 if you want to do that, you should use reactive form that makes dynamically. 如果你想这样做,你应该使用动态制作的反应形式。 you can look at the document : 你可以看一下这个文件:

https://angular.io/guide/dynamic-form https://angular.io/guide/dynamic-form

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

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