js -建议不是一个函数

Typeahead.js - suggestion is not a function

本文关键字:一个 函数 js      更新时间:2023-09-26

在与twitter typeahead.js进行了长时间的斗争之后,我终于到了决定应该使用哪个模板来提供建议的时候了。

但是,尽管这看起来像一个简单的过程,我得到以下错误:

Uncaught TypeError: g.t umplates .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中的变量,所以我不想这样做。

请提供解决这个问题的建议。

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

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

我正在关注@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;
        }
    });
});