Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 初始加载现有选项失败_Angular_Typescript_Angular Forms - Fatal编程技术网

Angular 初始加载现有选项失败

Angular 初始加载现有选项失败,angular,typescript,angular-forms,Angular,Typescript,Angular Forms,初始加载(现有的iu.装置)失败: 正在从服务器获取: getUnits(filter: ServerRequest) { return this._http.get(this.myAppUrl + 'Unit/GetUnits' + '?' + this.toQueryString(filter)) .pipe(map((res: Unit[]) => { return res; })); } 首先,select仅在下拉列表中显示其子项选项 因此,iu.uni

初始加载(现有的
iu.装置
)失败:

正在从服务器获取:

 getUnits(filter: ServerRequest) {
    return this._http.get(this.myAppUrl + 'Unit/GetUnits' + '?' + this.toQueryString(filter))
        .pipe(map((res: Unit[]) => { return res; }));
}

首先,
select
仅在下拉列表中显示其子项
选项

因此,
iu.unit
必须在
UI\u Units
中,否则默认情况下不会选择任何内容,并且列表中不会出现
iu.unit
选项

我们可以对此进行检查,我想让您关注第134行
writeValue
和第178行
\u getOptionId

writeValue(value: any): void {
  this.value = value;
  const id: string|null = this._getOptionId(value);
  // excerpt only
}

_getOptionId(value: any): string|null {
  for (const id of Array.from(this._optionMap.keys())) {
    if (this._compareWith(this._optionMap.get(id), value)) return id;
  }
  return null;
}
使用
ngModel
将值传递到元素时,调用
writeValue
,然后在包含所有子选项的映射中查找该值。如果未找到表示该值的选项(在您的案例中为
iu.unit
),则不会发生任何有意义的情况

最重要的是,默认情况下,该选项不会出现在select元素下,只有当它显式包含在模板中时才会出现


我已经创建了您试图完成的内容,请注意,
iu.unit
是数组
UI\u Units
中的一个元素。这就是为什么。

您可以共享您的component.ts代码吗?存储在
iu.unit
中的值必须是===到UI\u单位中的一个值。它一定是这些对象之一。不是另一个具有相同属性的对象。更新了我的postCan我可以使用另一个控件(角度材质、ng引导…),在那里我可以实现这一点?
是否
iu.Units
已经存在于
iu\u Units
中?你能提供一个stackblitz的例子吗?我预先准备了一个Stacklitz的例子:,可以吗,或者你需要更多的信息?@starting再次注意我只改变了
iu.unit
的初始化方式,如上所述,要选择,它必须严格等于选项的值之一。
 getUnits(filter: ServerRequest) {
    return this._http.get(this.myAppUrl + 'Unit/GetUnits' + '?' + this.toQueryString(filter))
        .pipe(map((res: Unit[]) => { return res; }));
}
writeValue(value: any): void {
  this.value = value;
  const id: string|null = this._getOptionId(value);
  // excerpt only
}

_getOptionId(value: any): string|null {
  for (const id of Array.from(this._optionMap.keys())) {
    if (this._compareWith(this._optionMap.get(id), value)) return id;
  }
  return null;
}