无法使用Angular JS获得选定选项

时间:2014-04-05 06:18:25

标签: angularjs

使用Angular JS获取select标签的选定选项。我得到的属性值为“1,2 ..”,我需要的是“一,二......当我更改选择标签时。

我的示例代码是

JS

var app = angular.module('myApp', []);
app.controller('myCtrl', function ($timeout, $scope) {
$scope.countList = [
    { id: 1, name: 'One' },
    { id: 2, name: 'Two' },
    { id: 3, name: 'Three' }
];
$scope.countSelected = $scope.countList[0].id;    
alert('Selected count ID: ' + $scope.countSelected);

    $scope.onchange = function(id) {
    alert("id:"+id);
    }
});

HTML

<div ng-controller="myCtrl">
Count:
<select ng-model="countSelected" 
        ng-options="count.id as count.name for count in countList" ng-change="onchange(countSelected)">
</select>
</div>

这是fiddle

2 个答案:

答案 0 :(得分:8)

试试这个

http://jsfiddle.net/TXJE6/8/

<div ng-controller="myCtrl">
    Count:
    <select ng-model="countSelected" 
    ng-options="count.id as count.name for count in countList" ng-change="onchange(countList[countSelected-1])">
    </select>
</div>

var app = angular.module('myApp', []);
app.controller('myCtrl', function ($timeout, $scope) {
    $scope.countList = [
        { id: 1, name: 'One' },
        { id: 2, name: 'Two' },
        { id: 3, name: 'Three' }
    ];
    $scope.countSelected = $scope.countList[0].id;    
    alert('Selected count ID: ' + $scope.countSelected);

    $scope.onchange = function(id) {
        alert("id:"+id.name);
    }
});

答案 1 :(得分:-8)

在调用控制器或承诺完成加载数据时,只需在下拉列表中绑定jquery更改

 $('#ddl').change(function () {
            console.log($('#ddl option:selected').val());
 });