Angular NG-AUTOCOMPLETE如何从对象数组中加载数据
很抱歉,如果标题不是很好的自我解释,实际上我遵循了 Angular-ng-autocomplete 它可以很好地运行,我可以加载国家/地区的数据,并且可以轻松地制定自动完成功能。但是,在我的原始项目代码中,我定义了一系列对象,如下所示,
assetDataDictionary: Array<DataDictionaryEntry> = [];
datadictionaryEntry 具有以下声明,
export interface DataDictionaryEntry {
id: string,
text: string
}
edit:datadictionaryEntry
在我键入时 从数据库中获取值在 [data] =“ AssetDatadictionary” 内部ng-autocomplete内部,我什么都没有。我一直在选择标签中使用以下代码来填充下拉列表,并且它效果很好,
<select id="assetId" name="id" class="form-control" [(ngModel)]="selectedAsset" (change)="getAssetValue()">
<option *ngFor="let asset of assetDataDictionary" value="{{asset.id}}-{{asset.text}}">{{asset.text}}</option>
</select>
所以我该如何替换让AssetDatectionary的资产与一起工作ng-autocomplete
希望我的问题有意义,有人能够帮助我。
谢谢你的宝贵时间。
Apologies if the title isn't very self explanatory, actually I followed the example of angular autocomplete from angular-ng-autocomplete and it works perfectly fine, I can load the data of countries and can work out the Autocomplete feature easily. However, in my original project code, I have defined an Array of Objects as follows,
assetDataDictionary: Array<DataDictionaryEntry> = [];
The DataDictionaryEntry has the following declaration,
export interface DataDictionaryEntry {
id: string,
text: string
}
EDIT: The DataDictionaryEntry gets value from Database
When I type in [data]="assetDataDictionary" inside ng-autocomplete, I do not get anything. I have been using the following code inside a select tag to populate a dropdown and it works perfectly fine,
<select id="assetId" name="id" class="form-control" [(ngModel)]="selectedAsset" (change)="getAssetValue()">
<option *ngFor="let asset of assetDataDictionary" value="{{asset.id}}-{{asset.text}}">{{asset.text}}</option>
</select>
So how can I replace let asset of assetDataDictionary to work with ng-autocomplete
Hope my question makes sense and someone is able to help me out with it.
Thank you for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来唯一的问题是您的数据具有
文本
字段,而不是name
字段,例如自动完成组件的示例。因此,您只需要进行几个更改。
首先,该组件接受
searchKeyword
输入:这是它用于过滤数据的属性名称。
在示例中,它设置为
name
,因此您需要将组件类的关键字
属性设置为text> text
而不是:然后您需要更新
#itemtemplate
因此,它显示您的text
属性而不是示例的name
属性:在这里显示它是一个stackblitz 更改。
It looks like the only problem here is that your data has a
text
field rather than aname
field like the autocomplete component's example.So there are just a couple changes you need to make.
First, the component accepts a
searchKeyword
Input:That's the property name it uses to filter data.
In the example, it's set to
name
, so you need to set your component class'skeyword
property totext
instead:Then you need to update the
#itemTemplate
so it displays yourtext
property rather than the example'sname
property:Here's a StackBlitz showing it working with those changes.