javascript - Angular 7: Cannot find control with name: formControlName in angular reactive form - Stack Overflow

After updating chrome to 80 I get the error Cannot find control with name: formControlName or Cannot fi

After updating chrome to 80 I get the error Cannot find control with name: formControlName or Cannot find control with path. 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.

After updating chrome to 80 I get the error Cannot find control with name: formControlName or Cannot find control with path. 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.

Share Improve this question asked Feb 7, 2020 at 8:40 Anouar TijaniAnouar Tijani 711 silver badge2 bronze badges 4
  • you forget the [formGroup]="myForm"? what is FormControl['items'], do you want to say myForm.get('items')? – Eliseo Commented Feb 7, 2020 at 8:52
  • Same for me - if I remove the formGroupName it seems to work OK again but it's not a fix – ChrisH Commented Feb 7, 2020 at 9:28
  • 2 github./angular/angular/issues/35190 – devnull Commented Feb 7, 2020 at 9:40
  • Underneath, the code calls _find method in forms.js. This uses Array.reduce. It may be this issue bugs.chromium/p/chromium/issues/detail?id=1049982 – Adam Marshall Commented Feb 7, 2020 at 10:59
Add a ment  | 

1 Answer 1

Reset to default 7

The core of the problem es from Array.reduce() in chromium version>=80. reactive forms find control method rely on reduce function (See _find() code below).

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 polyfillArray.reduce() function in versions >= 80.

In polyfills.ts or in main.ts right before bootstrapModule call.

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./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/p/chromium/issues/detail?id=1049982

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744733131a4590582.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信