0

i don't get value of the selected item in controller

Html file

<select data-ng-model="Employee" data-ng-options="c.Id as c.FirstName for c in Employees" ng-change="sortData()">
    <option value="">-- Select Employee Name --</option>
</select>

controller.js

$scope.sortData = function ()
{
    alert($scope.Employee);
};

How can i get the value of the selected Employee?

1
  • do you get a blank alert ? Commented Jun 1, 2015 at 13:20

2 Answers 2

1

The problem is occurring because the selected value is not able to update the scope. So, to tackle this, you can pass the selected value in you ng-change method as argument and update the scope as following :

HTML :

<select data-ng-model="employee" 
        data-ng-options="c.Id as c.FirstName for c in Employees" 
        ng-change="sortData(c)">
    <option value="">-- Select Employee Name --</option>
</select>

Controller :

$scope.sortData = function (selectedEmployee)
{
    $scope.employee = selectedEmployee;
    alert($scope.employee);
};

I hope this will do the trick.

Sign up to request clarification or add additional context in comments.

Comments

0

So you could use $scope.$watch which will return you the object you selected :

In your html :

    <select ng-model="Employee" ng-options="c as c.FirstName for c in Employees" >
        <option value="">-- Select Employee Name --</option>
    </select>

In your controller :

  $scope.Employees= [{Id:1,FirstName:'jean'},{Id:2,FirstName:'Bernard'}]; // I took this data Exemple but change it like you need 


   $scope.$watch('Employee',function(newEmp){
    console.log(newEmp.FirstName); // Display the FirstName
    // Or alert :    alert(newEmp.FirstName);

  });

Working codePen

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.