Angular 7: не удается найти элемент управления с именем: formControlName в angular реактивной форме - PullRequest
5 голосов
/ 07 февраля 2020

После обновления chrome до 80 появляется ошибка. Не удается найти элемент управления с именем: formControlName или Не удается найти элемент управления с путем. Мы ничего не изменили в коде, теперь проблема возникает везде, где мы используем контрольное имя, у кого-то есть идея?

Пример

<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>

Это всего лишь пример того, как мы используйте FormControls.

1 Ответ

5 голосов
/ 07 февраля 2020

Суть проблемы заключается в Array.reduce() в версии с хромом> = 80 . метод управления поиском реактивных форм полагается на функцию 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);
}

В качестве грязного обходного пути можно использовать функцию polyfill Array.reduce() в версиях> = 80 .

В polyfills.ts или main.ts прямо перед bootstrapModule вызов.

Добавить код ниже

(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;
    };
  }
})();

Кредиты: mix5003 - https://github.com/angular/angular/issues/35219

Еще один обходной путь (кажется, он тоже работает)

(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);
    }
  });
})();

Источник: https://bugs.chromium.org/p/chromium/issues/detail?id=1049982

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...