Typeahead.js - suggestion is not a function

3

经过长时间与Twitter的typeahead.js的斗争,我终于到了决定使用哪个模板来进行建议的阶段。

但是尽管这似乎是一个直截了当的过程,我却遇到了以下错误:

Uncaught TypeError: g.templates.suggestion is not a function

我的代码如下:

HTML:

<input id = "search" type="text" placeholder="Colors">

Javascript:

var colors = ["red", "blue", "green", "yellow", "brown", "black"];

var colorsource = new Bloodhound({
  datumTokenizer: Bloodhound.tokenizers.whitespace,
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  // `states` is an array of state names defined in "The Basics"
  local: colors
});

$('#search').typeahead({
  hint: true,
  highlight: true,
  minLength: 1
},
{
  name: 'color',
  source: colorsource,
  templates: {
  empty: [
    '<div class="empty-message">',
      'unable to find any Best Picture winners that match the current query',
    '</div>'
  ].join('\n'),
  suggestion: '<div>{{color}}</div>'
}
});

我看到过使用Handlebars.js编译模板的示例,但我计划在我的建议中使用Angular.js中的变量,因此我不想这样做。如果对如何解决这个问题有任何建议,将不胜感激。
2个回答

15

您的建议选项必须是一个返回HTML的函数,例如:

...
suggestion: function(e) { return ('<div>' + e.value + '</div>'); }
...

0

我正在关注@potatopeelings,但是建议没有出现。

这是我的代码

$(document).ready(function () {
    $("#penilai").typeahead({
        source: function (query, process) {
            var countries = [];
            map = {};

            return $.post('/Evaluation/JsonSenaraiPenilai', { query: query }, function (data) {
                $.each(data, function (i, country) {
                    map[country.Evaluator_name] = country;
                    countries.push(country.Evaluator_name);
                });
                process(countries);
            });
        },
        templates: {
            empty: [
                '<div class="empty-message">',
                'unable to find any Best Picture winners that match the current query',
                '</div>'
            ].join('\n'),
            suggestion: function (e) { return ('<div>' + e.Evaluator_name + '-' + e.Evalator_staf_no + '</div>'); }
        },
        updater: function (item) {
            var selectedShortCode = map[item].Evalator_staf_no;
            $("#evalutor").val(selectedShortCode);
            return item;
        }
    });
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接