<div ng-app="my_app" ng-controller="my_ctrl">
<select ng-model="students" ng-options="x.name for x in details" ng-change="onChange()" >
</select>
<span>Selected Student = {{students}}</span> ,
<span>Selected Mark = {{students.mark}}</span>
</div>
<script>
var app = angular.module('my_app', []);
app.controller('my_ctrl', function($scope, $http) {
$http.get("student-data.php") // data source for options
.then(function (response) {$scope.details = response.data.student_data;});
$scope.onChange = function()
{
var id=$scope.students.id;
//alert(id);
}
});
</script>
OnChange event and Passing Data to Back end script
We will pass the selected id and name to a back end script in PHP and then return the same with additional message. Our PHP script will receive id and name and then return the string with additional string added to this.
DEMO: onChange Event to post selected option to back end script→
We have to post the data ( selected from the drop down list ) and then receive the response from the PHP page and display the same.
This is our PHP code, we are using GET method to receive the data from URL.
<?Php
$id=$_GET['id'];
$name=$_GET['name'];
echo "Welcome $name , your id is : $id";
?>
Using Filter
In all above code we have used JSON string or MySQL database as source for our options. After getting the data as options we can add filter to restrict the choice of options for selection.
In this demo we have restricted the options to class=Three only. Note that the filter is applied to available options only and full data is included in the JSON string.
DEMO: Filtering Options →
Filtering by using Function
Instead of using value directly we can use functdion to add more conditions to our filtering. We can use variables also so filtering conditions can change dynamically.
DEMO: Filtering Options using functions →