E
E
Eugene2015-12-25 14:40:05
Angular
Eugene, 2015-12-25 14:40:05

How to attach yandex suggest view to angular?

<input type="text"
                           class="form-control"
                           id="suggest"
                           placeholder="Адрес"
                           ng-model="suggest"
                            >
{{ suggest }}
<script src="//api-maps.yandex.ru/2.1/?lang=ru_RU&load=SuggestView&onload=onLoad"></script>
                <script>
                    function onLoad (ymaps) {
                        var suggestView = new ymaps.SuggestView('suggest');
                    }
                </script>

The problem is that in the model there is only the value that was entered before the autocomplete.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sargonpiraev, 2017-04-24
@sargonpiraev

function yandexSuggestViewDirective () {
  return {
    restrict: 'A',
    require: [ 'ngModel' ],
    scope: { ngModel: '=' },
    link: function (scope, element) {
      ymaps.ready(function () {
        new ymaps.SuggestView(element[0]).events.add('select', function (e) {
          scope.$apply(function () {
            scope.ngModel = e.get('item').value;
          });
        });
      })
    }
  };
}

<input class="form-control" placeholder="Адрес" ng-model="suggest" yandex-suggest-view />

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question