【问题标题】:ngx-chips: unable to show auto complete itemsngx-chips:无法显示自动完成的项目
【发布时间】:2020-03-05 12:22:09
【问题描述】:

在 Angular 中,我想在自动完成中显示项目 (genreName)。在.html我写了以下内容:

 <tag-input [ngModel]="genre" [onlyFromAutocomplete]="true">
            <tag-input-dropdown [showDropdownIfEmpty]="false"
                                [autocompleteItems]="responseRawGenreList">
            </tag-input-dropdown>
</tag-input>

.ts文件中,我写了如下代码:

this.commonService.getAllGenre(this.userToken).subscribe((data: any) => {
  this.responseRawGenreList = data.data;
  console.log(this.responseRawGenreList)
});

上面的代码安慰结果如下:

[0 … 99]
0: {genreId: 4, genreName: "Action", genreDescription: "", status: "ONE"}
1: {genreId: 5, genreName: "Action", genreDescription: "", status: "ONE"}
2: {genreId: 6, genreName: "Action", genreDescription: "", status: "ONE"}
3: {genreId: 7, genreName: "ऐक्शन", genreDescription: "", status: "ONE"}

当我在浏览器的“输入标签”中输入内容时,它会显示以下错误:

core.js:6014 错误类型错误:无法读取未定义的属性“toString” 在 TagInputDropdown.matchingFn (ngx-chips.js:207) 在 ngx-chips.js:1216 在 Array.filter() 在 TagInputDropdown.getMatchingItems (ngx-chips.js:1208) 在 SafeSubscriber.TagInputDropdown.show [as _next] (ngx-chips.js:1011) 在 SafeSubscriber.__tryOrUnsub (Subscriber.js:183) 在 SafeSubscriber.next (Subscriber.js:122) 在 Subscriber._next (Subscriber.js:72) 在 Subscriber.next (Subscriber.js:49) 在 FilterSubscriber._next (filter.js:33)

【问题讨论】:

    标签: angular ngx-chips


    【解决方案1】:

    您需要添加[identifyBy][displayBy] 并传递属性名称。

    您可以设置:

    identifyByProperty = 'genreId';
    
    [identifyBy]="identifyByProperty"
    

    或者,

    identifyBy="genreId"
    

    试试这样:

    <tag-input [ngModel]="genre" [onlyFromAutocomplete]="true">
        <tag-input-dropdown [autocompleteObservable]="requestAutocompleteItems" displayBy="genreName"
            identifyBy="genreId">
        </tag-input-dropdown>
    </tag-input>
    

    Working Demo

    【讨论】:

    • >未捕获(承诺中):NullInjectorError: StaticInjectorError(AppModule)[TagInputComponent]:
    • &lt;tag-input-dropdown [autocompleteObservable]="responseRawGenreList" [displayBy]="genreName" [identifyBy]="genreId" &gt; &lt;/tag-input-dropdown&gt;
    • 如下更改此属性并尝试 [identifyBy]="'genreId'" [displayBy]="'genreName'"
    • @NiladriBanerjee-Uttarpara 试试这样:displayBy="genreName" identifyBy="genreId"。查看我分享的演示
    • 没关系,!但是,如何在表单提交时验证 ngx-chips(输入标签)?意思是我要检查输入标签是否为空?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-24
    • 2012-06-12
    • 1970-01-01
    相关资源
    最近更新 更多