In AngularJS how to use datalist

user4277191 picture user4277191 · Apr 9, 2015 · Viewed 26.6k times · Source
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>

<div ng-app="" ng-controller="cntryController">
    <input list="testList" type="" ng-model="SelectedDoctor" ng-change="LoadSessionData(SelectedDoctor)" />
    <datalist id="testList">
        <option value="Dr.Test1" ng-selected="selected"></option>
        <option value="Dr.Test2"></option>
        <option value="Dr.Test2"></option>
    </datalist>
</div>

Controller

function cntryController($scope) {
    $scope.LoadSessionData = function(val) {
        console.log(val);
    };
}

check this Link http://jsbin.com/jifibugeke/1/edit?html,js,console,output

Above mention datalist sample code and URL using angularjs, here my problem is what ever i am typing the text box, in controller add by added every words,in my requirement in datalist selected details only shows in controller,

Answer

Anandha Mariappan A picture Anandha Mariappan A · Jul 3, 2015
//here your html
<div ng-app="myapp1" ng-controller="cntryController">
    <input list="testList" type="" ng-model="SelectedDoctor" ng-change="LoadSessionData(SelectedDoctor)" />
    <datalist id="testList">
        <option ng-repeat="x1 in names" value="{{x1.drname}}">
    </datalist>
</div>

//here your script        
<script>
    var app=angular.module('myapp1',[]);
    app.controller('cntryController',function ($scope) {
//data for ng-repeat
    $scope.names=[{'drname':'Dr.Test1'},{'drname':'Dr.Test2'},{'drname':'Dr.Test3'}]
    $scope.LoadSessionData = function(val) {
//console log
        console.log(val);
    }
});
</script>