Symfony-Twig TypeAhead 函数

Symfony-Twig TypeAhead Function

我正在尝试实现 typeAhead 函数。我正在使用 Symfony 框架。但是,如果似乎不起作用。 我得到的错误是

Uncaught ReferenceError: Bloodhound is not defined

我对 symfony 和 twig 还很陌生。谁能告诉我为什么代码不起作用?

树枝

<div id="prefetch">
    <input class="typeahead" type="text" placeholder="Countries">
</div>
<script type="application/javascript">
    var countries = new Bloodhound({
        datumTokenizer: Bloodhound.tokenizers.whitespace,
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        // url points to a json file that contains an array of country names, see
        // https://github.com/twitter/typeahead.js/blob/gh-pages/data/countries.json
        prefetch: '{{ path('typeAhead') }}'
    });

    // passing in `null` for the `options` arguments will result in the default
    // options being used
    $('#prefetch .typeahead').typeahead(null, {
        name: 'countries',
        source: countries
    });
</script>

控制器(响应)

/**
 * @Route("/typeAhead", name="typeAhead")
 */
public function typeAheadAllAction(Request $request)
{
    $products=$this->getDoctrine()->getRepository(products::class)->findAll();

    $pr_name=array();
    foreach($products as $product){
        $name=$product->getName();
        array_push($pr_name,$name);
    }

    return new Response(json_encode(array($pr_name)),100);
}

控制器(渲染 twig 文件)

/**
 * @Route("/test", name="test")
 */
public function testAction(Request $request)
{
    return $this->render('typeahead.html.twig', array(
    ));

}

欢迎来到 Stack Overflow。

也许您正指向 this example page,并且您想要实施 Bloodhood(typeAhead 的建议引擎)。

如果是这样,请考虑添加同时具有 typeAhead 和 Bloodhood 的 typeahead.bundle.js(您可以检查页面并查看下面的 link jQuery):

<script src="https://twitter.github.io/typeahead.js/releases/latest/typeahead.bundle.js"></script>