解决方案:
我们在这里解决了 2 个问题:
1) httpt.get分配错误,我们收到错误消息:
ORIGINAL EXCEPTION: Cannot find a differ supporting object
'[object Object]' of type 'object'.
NgFor only supports binding to Iterables such as Arrays.
2) Bootstrap-Select-Box 的时序问题;选项字段为空
1) 错误的分配
以下分配是错误的:
this.agency = this.ws.get(serviceUrls.agency)
.subscribe(
(data:any) => this.agency = data,
(err:any) => console.debug('AGENCY ERROR:',err),
() => console.debug(this.agency)
);
并给出输出:
[对象,对象,对象,对象,..]
不能通过ngFor迭代的内容应更正为:
this.ws.get(serviceUrls.agency)
.subscribe(
(data:any) => **this.agency = data**, // <-- ONLY HERE
(err:any) => console.debug('AGENCY ERROR:',err),
() => console.debug(this.agency)
);
为了完整起见,这是ws.get-method:
get(url: string) {
return this.http.get(url)
.map((data:any) => data.json());
}
这是该机构:
agency: any[];
2) BOOTSTRAP-SELECT 和 ANGULAR2 的时序问题
那是 Bootstrap-Select-Box (一点点 - 关于 CI - 改编版本):
<select [(ngModel)]="localValues.agency"
name="agency"
id="agency"
class="selectpicker select-search"
data-selectpicker
data-live-search="true"
required="">
<option
*ngFor="let item of agency"
[ngValue]="item.value">
{{ item.label }}
</option>
</select>
selectpicker 的激活发生在:
ngAfterViewChecked () {
//noinspection TypeScriptUnresolvedFunction
$('.selectpicker').selectpicker();
}
但是缺少一件重要的事情;选择框是在可观察对象发出数据之前呈现的。因此我们必须先检查是否存在代理,然后渲染该框:
ngAfterViewChecked () {
if (this.agency) { // <-- IMPORTANT!
//noinspection TypeScriptUnresolvedFunction
$('.selectpicker').selectpicker();
}
}
就这样!也许这些信息对你也有用;)