How to use angularjs ng-click with html5 datalist
Asked Answered
H

3

7

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?

Hypnotherapy answered 12/3, 2014 at 2:42 Comment(3)
You probably need an ng-model in your datalist. I know that it wouldn't work if it were a select tag without an ng-model.Tutti
not work.. here is en example jsfiddle.net/lukers/MCnL2Hypnotherapy
Try my answer here. #23113162Hoopoe
A
5

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.

Anticlockwise answered 23/4, 2015 at 9:33 Comment(0)
H
2

datalist is same as select, you don't put the event handler in option, you put the event handler in select or input. Also you don't use ng-click, you use ng-change for this.

Hithermost answered 12/3, 2014 at 3:1 Comment(2)
i put the event in datalist and not work.. here is en example jsfiddle.net/lukers/MCnL2Hypnotherapy
I said both ng-change, ng-model needs put in input or select, Do Not put in datalist or option.Hithermost
L
1
<input list="stateList"  name="state" ng-model = "payCntrl.billingAddressService.billingAddress.state">
    <datalist id="stateList">
        <select>
            <option ng-repeat="state in payCntrl.billingAddressService.states | filter : {country : payCntrl.billingAddressService.billingAddress.country}" 
                                    value="{{state.name}}"></option>
       </select>    
   </datalist>
  • The name and ng-model are on the input .

    Data :

    states : [{name : "NJ" , country: "USA"} ,  {name : "NY" , country: "USA"} , 
             {name : "GA" , country: "USA"} ,   {name : "TX" , country: "USA"} , 
             {name : "CA" , country: "USA"} , 
             {name : "Delhi" , country: "India"} ,  {name : "Karnataka" , country: "India"} , 
             {name : "Hyderabad" , country: "India"} ,  {name : "West Bengal" , country: "India"} , 
             {name : "Zhejiang" , country : "China"} , {name : "Hubei" , country : "China"},
             {name : "LONDON" , country : "United Kingdom"} , {name : "MANCHESTER" , country : "United Kingdom"}] 
    
Lava answered 28/1, 2015 at 20:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.