How to use angularjs ng-click with html5 datalist

lukersf picture lukersf · Mar 12, 2014 · Viewed 9.1k times · Source

I'm working with AngularJS and i want to use the directive ng-click when an element of a datalist (html5) is selected.

Here is an example of my actual code:

<label>Search</label>
<input type="text" class="input-search" list="datalistcit" ng-change="changeQuery(queryCity)" ng-model="queryCity" />
<datalist id="datalistcit">
   <option ng-repeat="city in cities" ng-click="goCity(city)" value="{{city.name}}">
   </option>
</datalist>

It doesn't work, never execute the js method goCity.. any idea?

Answer

Stephan Muller picture Stephan Muller · Apr 23, 2015

ng-click won't work on datalist options, as the click event (nor any of the keypress events) doesn't seem to be fired when using the datalist.

You will have to use your ng-change on the function input and extend that to check if the current value is also present in the datalist.