0

JSFiddle: https://jsfiddle.net/ealonwang/7y25ru40/

I am implementing the typeahead function. It seems that ng-model does not work with autocomplete.

Here is my code.

HTML:

<input type="text" id="origin" ng-model="searchForm.origin" placeholder="City, State">

AngularJS:

var origin = ["DALLAS, TX", "DALLAS, NE"];

$("#origin").autocomplete({
     source: origin,
     autoFocus: true,
     delay: 0,
     minLength: 3
 });

When I type DAL in the input and select DALLAS, TX from the dropdown list, I actually get DAL for ng-model. Anyone has a solution? Thanks in advance.

enter image description here

3
  • Why not use an angular-specific dropdown? Commented Feb 17, 2016 at 21:07
  • @DanielHigueras This is for typeahead feature. Because I am doing company project, so I cannot use Angular Bootstrap, that's why I use jQuery autocomplete. Commented Feb 17, 2016 at 21:11
  • Maybe this helps stackoverflow.com/questions/12959516/… Commented Feb 17, 2016 at 21:54

1 Answer 1

1

I have to create a directive for this.

app.directive("autoComplete", function ($timeout) {
        return {
            restrict: "A",
            link: function (scope, element) {
                var location = ["OMAHA, NE", "OMAHA, TX", "DALLAS, TX", "DALLAS, NE"];

                element.autocomplete({
                    source: location,
                    autoFocus: true,
                    delay: 0,
                    minLength: 3,
                    select: function () {
                        $timeout(function () {
                            element.trigger("input");
                        }, 0);
                    }
                });
            }
        }
    });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.