如何为Angular 2 ng引导设置预排默认值/初始值?

如何为Angular 2 ng引导设置预排默认值/初始值?,angular,ng-bootstrap,Angular,Ng Bootstrap,我使用ng-bootstrap.github.io中的模板作为结果示例 这个很好用。我使用它让用户在表单中输入城市 问题在于编辑用户详细信息。我不知道如何为该用户填充以前保存的城市。这是我在HTML中的内容: <template #rt let-r="result" let-t="term"> {{ r.name}} </template> <input (blur)="lostFocus($event)" class="form-control" form

我使用ng-bootstrap.github.io中的模板作为结果示例

这个很好用。我使用它让用户在表单中输入城市

问题在于编辑用户详细信息。我不知道如何为该用户填充以前保存的城市。这是我在HTML中的内容:

<template #rt let-r="result" let-t="term">
    {{ r.name}}
</template>
<input (blur)="lostFocus($event)" class="form-control" formControlName="city_obj" type="text" [(ngModel)]="typeaheadModel" [ngbTypeahead]="search" [resultTemplate]="rt" [inputFormatter]="formatter" (selectItem) = "selectItem($event)">

{{r.name}}
这是来自组件:

search = (text$: Observable<string>) =>
text$
.debounceTime(200)
.map(term => term === '' ? []
    : this.cities.filter(v => new RegExp(term, 'gi').test(v.name)).slice(0, 10));
formatter = (x: {name: string}) => x.name;
search=(text$:Observable)=>
正文$
.debounceTime(200)
.map(术语=>术语==''?[]
:this.cities.filter(v=>newregexp(术语'gi').test(v.name)).slice(0,10));
格式化程序=(x:{name:string})=>x.name;
我使用模型驱动的表单

在数据库中,我只为用户保存城市id,因为城市位于单独的表中

因此,在表单生成器中,我有city_id字段和city_obj

API将只保存城市id,而忽略城市obj

我使用selectItem事件设置组件中的city\u id字段

那么,如何将以前保存的city设置为输入并保持超前打印功能呢?

是我自己发现的

我只需要将同一个对象设置到表单控件,但在debounceTime之后

编辑:因为这是很久以前的事了,我不知道这是不是正确的方法。由于人们对这个很感兴趣,我找到了我使用这个的项目。下面是我当时设置typeahead值所做的工作,它运行良好:

this.form.controls['city_obj'].setValue({id:this.formData.city.id, name:this.formData.city.name});

你能在解决问题后更新源代码吗@我在这里。你能更新源代码吗?我在“编辑”模式下遇到了同样的问题。不确定如何使用现有值设置typeahead。非常感谢。