0

The previous member wrote an angular js for autocomplete search.

When I type some keywords it will return me the candidates.

However when I nav those candidates with arrow keys, it will appear the product id in the input box, I want to show the product name.

How could I do it with angular js ?

item.label is product name, and item.value is product id

I'm not familiar with it, thanks so much

js

$(document).ready(function(){
    $("#search_bar").autocomplete({
        source: function (request, response) {
                 console.log(request['term']);
                 name = "name="+request['term'];
                 $.ajax({
                     url: "/v1/search/",
                     type: "GET",
                     data: name,
                     success: function (data) {
                        console.log(data);
                         response($.map(data, function (el) {
                             return {
                                 label: el.name,
                                 value: el.id
                             };
                         }));
                     }
                 });
            },
        focus: function (event, ui) {
            $("#search_bar").val(ui.item.label);
        },
        select: function (event, ui) {
            $("#search_bar").val(ui.item.label);
            $("#search_bar_id").val(ui.item.value);
            window.location.href = '/browse/'+ui.item.value;
            return false;
        }
    });
});
2
  • Here is auto-complete for angular and Demo Aug 27, 2015 at 8:15
  • 2
    Your code is not AngularJs, but looks very much like jQuery. Also, the change you request is trivial, so are you sure you know what you are doing and could incorporate a correct answer into your code?
    – dirkk
    Aug 27, 2015 at 8:38

1 Answer 1

0

angular-ui's typeahead works great! You can use this: https://github.com/angular-ui/bootstrap/tree/master/src/typeahead

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.