作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
将 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>
最佳答案
问题的核心来自Array.reduce()
在 Chrome 版>=80 . react 形式查找控制方法依赖于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);
}
Array.reduce()
版本 >= 80 中的功能。
polyfills.ts
或在
main.ts
就在
之前引导模块 称呼。
(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;
};
}
})();
(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);
}
});
})();
关于javascript - Angular 7 : Cannot find control with name: formControlName in angular reactive form,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60109837/
我是一名优秀的程序员,十分优秀!