简体   繁体   English

Angular 7:在角度反应形式中找不到名称为 formControlName 的控件

[英]Angular 7: Cannot find control with name: formControlName in angular reactive form

After updating chrome to 80 I get the error Cannot find control with name: formControlName or Cannot find control with path.将 chrome 更新到 80 后,我收到错误无法找到带有名称的控件:formControlName 或无法找到带有路径的控件。 We haven't changed anything in code, the problem now occurs everywhere we use from controle name, someone has an idea here?我们没有更改任何代码,现在问题出现在我们从控件名称使用的任何地方,有人在这里有想法吗?

Example例子

<div *ngIf="items$ | async; else loading_items">
<div class="list" id="subject-list">
<div class="item" formArrayName="items" *ngFor="let view_items of FormControls['items'].controls  | arrayTextFilter:[FormControls['search'].value, 'value.subject.name'] | orderBy:['-value.subject.priority', 'value.subject.name','value.subject.id']; let i = index;">
<div [formGroupName]="i" class="tem" [draggable] [dragScope]="'selected_items'+local_drop_id" [dragData]="view_items.value" (dragstart)="onDragStart($event)" (dragend)="onDragEnd($event)">
<div formGroupName="subject" class="subject_name">
    {{view_items.value.subject.name}}
</div>
<div class="item_select">
<select class="form-control" formControlName="count_of_items_part" >
<option *ngFor="let count_of_items_part of max_count_of_items_part | numberTo: [0]" [ngValue]="count_of_items_part">{{count_of_items_part}}</option>
</select>
<div *ngIf="view_items.controls['count_of_items_part'].errors && !view_items.controls['count_of_items_part'].pristine" class="error-msg">
<div [hidden]="!view_items.controls['count_of_items_part'].errors.min">message2</div>
<div [hidden]="!view_items.controls['count_of_items_part'].errors.max">message1</div>
</div>
</div>
<div class="clearfix"></div>
    </div>
    </div>
    </div>
    </div>

This is just an example of how we use FormControls.这只是我们如何使用 FormControls 的一个例子。

The core of the problem comes from Array.reduce() in chromium version>=80 .问题的核心来自Chromium v​​ersion>=80 中的Array.reduce() reactive forms find control method rely on reduce function (See _find() code below).反应形式的 find 控制方法依赖于 reduce 函数(参见下面的_find()代码)。

function _find(control, path, delimiter) {
        if (path == null)
            return null;
        if (!(path instanceof Array)) {
            path = path.split(delimiter);
        }
        if (path instanceof Array && (path.length === 0))
            return null;
        return path.reduce(function (v, name) { // <--- Array.Reduce() Call
            if (v instanceof FormGroup) {
                return v.controls.hasOwnProperty(name) ? v.controls[name] : null;
            }
            if (v instanceof FormArray) {
                return v.at(name) || null;
            }
            return null;
        }, control);
}

As a dirty workaround would be to polyfill Array.reduce() function in versions >= 80.一个肮脏的解决方法是在版本 >= 80 中 polyfill Array.reduce()函数。

In polyfills.ts or in main.ts right before bootstrapModule call.polyfills.tsmain.ts bootstrapModule调用之前。

Add the code below添加下面的代码

(function () {
  function getChromeVersion() {
    const raw = navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./);

    return raw ? parseInt(raw[2], 10) : false;
  }

  const chromeVersion = getChromeVersion();
  if (chromeVersion && chromeVersion >= 80) {
    Array.prototype.reduce = function (callback /*, initialValue*/) {
      'use strict';
      if (this == null) {
        throw new TypeError('Array.prototype.reduce called on null or undefined');
      }
      if (typeof callback !== 'function') {
        throw new TypeError(callback + ' is not a function');
      }
      let t = Object(this), len = t.length >>> 0, k = 0, value;
      if (arguments.length === 2) {
        value = arguments[1];
      } else {
        while (k < len && !(k in t)) {
          k++;
        }
        if (k >= len) {
          throw new TypeError('Reduce of empty array with no initial value');
        }
        value = t[k++];
      }
      for (; k < len; k++) {
        if (k in t) {
          value = callback(value, t[k], k, t);
        }
      }
      return value;
    };
  }
})();

Credits: mix5003 - https://github.com/angular/angular/issues/35219积分: mix5003 - https://github.com/angular/angular/issues/35219

Another workaround (Seems it's working as well)另一种解决方法(似乎也有效)

(function() {
  const arrayReduce = Array.prototype.reduce;
  let callback;
  Object.defineProperty(Array.prototype, 'reduce', {
    value: function(cb, ...args) {
    callback = cb;
      return arrayReduce.call(this, callback, ...args);
    }
  });
})();

Source: https://bugs.chromium.org/p/chromium/issues/detail?id=1049982来源: https : //bugs.chromium.org/p/chromium/issues/detail?id=1049982

暂无
暂无

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

相关问题 Angular pipe 与 angular 表单数组的反应 formControlName - Angular pipe with reactive formControlName of angular form array 对于不相关的模型字段,角度反应形式会抛出“无法找到带有名称的控件” - angular reactive form throws "Cannot find control with name" for irrelevant model field Angular:仅一种形式的多个字段ControlName,反应形式 - Angular: multiple fields in just one formControlName, reactive form Angular formControlName - 反应形式 - 将值设置为 primeNg p-dropdown - Angular formControlName - reactive form - Setting values to primeNg p-dropdown Angular form group:找不到具有未指定name属性的控件 - Angular form group: cannot find control with unspecified name attribute Angular 深度嵌套反应式表单:在嵌套的 FormArray 上找不到带有路径的控件 - Angular Deeply Nested Reactive Form: Cannot find control with path on nested FormArray 角度:找不到名称为“日期”的控件 - Angular: Cannot find control with name: 'date' 找不到具有名称的控件:'成分 angular 错误 - Cannot find control with name: 'ingredients angular error Angular Forms:在 Angular Z6450242531912982A6683CAE88A 中找不到表单控件 - Angular Forms: Cannot find form control in Angular Forms 用于输入类型文件的反应式上传表单的 Jest 测试用例<input type="file" formControlName="userslist"> (角 7) - Jest Test Case for reactive upload form for input type file <input type="file" formControlName="userslist"> (Angular 7)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM