我没有获得控制器中所选项目的值
Html文件
<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);
};
如何获得所选员工的价值?
答案 0 :(得分:1)
问题出现是因为所选值无法更新范围。因此,要解决此问题,您可以将ng-change方法中的选定值作为参数传递,并更新范围如下:
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>
控制器:
$scope.sortData = function (selectedEmployee)
{
$scope.employee = selectedEmployee;
alert($scope.employee);
};
我希望这会成功。
答案 1 :(得分:0)
所以你可以使用$ scope。$ watch来返回你选择的对象:
在你的HTML中:
<select ng-model="Employee" ng-options="c as c.FirstName for c in Employees" >
<option value="">-- Select Employee Name --</option>
</select>
在您的控制器中:
$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);
});