Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use angularjs ng-click with html5 datalist

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?

like image 610
lukersf Avatar asked Mar 12 '14 02:03

lukersf


People also ask

Is Datalist tag in HTML5?

The datalist tag is introduced in HTML5. The <datalist> tag should be used with an <input< element that contains a "list" attribute. The value of "list" attribute is linked with the datalist id.

What is Ng-click in AngularJS?

The ng-click directive tells AngularJS what to do when an HTML element is clicked.

What is the difference between Ng-click and Onclick?

Another significant difference between ng-click and onclick is the execution context. Code inside an onclick attribute executes against the global window object, while an expression inside of ng-click executes against a specific scope object, typically the scope object representing the model for the current controller.

Is Datalist supported by all browsers?

Unfortunately, Internet Explorer and Chrome are the only browsers to support datalists on range inputs at this time.


1 Answers

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.

like image 186
Stephan Muller Avatar answered Oct 16 '22 09:10

Stephan Muller