【问题标题】:Not able to get selected option using Angular JS无法使用 Angular JS 获得选定的选项
【发布时间】:2014-05-17 15:00:01
【问题描述】:

正在使用 Angular JS 获取选择标签的选定选项。我得到的属性值为“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

【问题讨论】:

    标签: angularjs


    【解决方案1】:

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

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

    【讨论】:

    • 最好使用现有的 Angular 功能而不是使用 Jquery 解决方法。
    【解决方案2】:

    试试这个

    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);
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-30
      相关资源
      最近更新 更多