Angular NG-AUTOCOMPLETE如何从对象数组中加载数据

发布于 2025-02-03 09:14:40 字数 1134 浏览 4 评论 0原文

很抱歉,如果标题不是很好的自我解释,实际上我遵循了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ヤ经典坏疍 2025-02-10 09:14:40

看来唯一的问题是您的数据具有文本字段,而不是name字段,例如自动完成组件的示例。

因此,您只需要进行几个更改。

首先,该组件接受searchKeyword输入:

<ng-autocomplete
  [data]="countries"
  [searchKeyword]="keyword" <---------
>
</ng-autocomplete>

这是它用于过滤数据的属性名称。

在示例中,它设置为name,因此您需要将组件类的关键字属性设置为text> text而不是:

export class YourAwesomeComponent {
  keyword = 'text'; // <--- ng-autocomplete's searchKeyword
}

然后您需要更新#itemtemplate因此,它显示您的text属性而不是示例的name属性:

<ng-template #itemTemplate let-item>
  <a [innerHTML]="item.text"></a> <--- change this from item.name to item.text
</ng-template>

在这里显示它是一个stackblitz 更改。

It looks like the only problem here is that your data has a text field rather than a name 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:

<ng-autocomplete
  [data]="countries"
  [searchKeyword]="keyword" <---------
>
</ng-autocomplete>

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's keyword property to text instead:

export class YourAwesomeComponent {
  keyword = 'text'; // <--- ng-autocomplete's searchKeyword
}

Then you need to update the #itemTemplate so it displays your text property rather than the example's name property:

<ng-template #itemTemplate let-item>
  <a [innerHTML]="item.text"></a> <--- change this from item.name to item.text
</ng-template>

Here's a StackBlitz showing it working with those changes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文