【问题标题】:Getting ngTagsInput to work with answer from elasticsearch让 ngTagsInput 与来自 elasticsearch 的答案一起使用
【发布时间】:2015-11-14 13:54:34
【问题描述】:

我使用 elasticsearch 来存储标签,并希望 ngTagsInput 动态获取它们以进行自动完成。它工作到 ngTagsInput 收到来自 elasticsearch 的答案,我在浏览器控制台中收到以下错误:

Error: [ngRepeat:dupes] Duplicates in a repeater are not allowed. Use
'track by' expression to specify unique keys. Repeater: item in 
suggestionList.items track by track(item), Duplicate key: undefined, 
Duplicate value:{"_index":"data","_type":"tags","_id":"4","_score":1,"_source":{"id":4,"text":"Testinchen"}}

这里的问题是我收到的承诺包含一个包含 4 个元素的数组(如果我有更多的声誉,我会在这里发布一张图片,但是不,堆栈溢出不会让我:x)

我整晚都在尝试让 ngTagsInput 接受这个承诺,而我设法做的最好的事情就是以某种方式检索我的对象,如果弹性搜索只有一个命中而不是多个命中。

HTML 代码如下所示:

<div data-ng-controller="TagsController">
    <tags-input ng-model="taglist">
        <auto-complete source="loadTaglist($query)"template="tagTemplate"></auto-complete>
    </tags-input>
</div>
<script type="text/ng-template" id="tagTemplate">
    <span ng-bind-html="$highlight($getDisplayText())"></span>
    <div ng-repeat="model in data._source">
        @{{model}}
    </div>
</script>

这里 {{ model }} 前面的 @ 是因为它运行在 laravel 后端,所以没关系^^

而JS如下:

petopia.controller('TagsController', function($scope, tags, esClient) {

    $scope.taglist = [{
        "id" : "1",
        "text": "Pet"
    }];

    $scope.loadTaglist = function(query) {
        var taglist = esClient.search({
            q: "*"+query+"*"
        }).then(function (response) {
            return response.hits.hits;
        }, function (error) {
            console.trace(error.message);
        });

        return tags.load(taglist);
    };
});

petopia.service('tags', function($q) {
    this.load = function(taglist) {

        var deferred = $q.defer();
        deferred.resolve(taglist);
        console.log(deferred.promise);
        return deferred.promise;
    };
});

任何帮助都将不胜感激,因为作为一个对 angularjs 很陌生的人,我在这里很茫然。

【问题讨论】:

    标签: javascript angularjs ng-tags-input


    【解决方案1】:

    当您的数组中有重复项时,您应该通过索引对其进行跟踪。

    ng-repeat="model in data._source track by $index"

    【讨论】:

    • 是的,我试过了,但问题甚至在数据到达模板之前就出现了。如果elasticsearch只发送一个命中,它有点工作,除了模型然后包含id和文本作为字符串。不过感谢您的快速回复!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-27
    • 1970-01-01
    • 2018-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多