无法使用Angular JS获得选定的选项 [英] Not able to get selected option using Angular JS

查看:77
本文介绍了无法使用Angular JS获得选定的选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在使用Angular JS来获取select标签的selected选项.我得到的属性值为"1,2 ..",当我更改选择标记时,我需要的是"1、2 ..".

Working on getting the selected option of a select tag using Angular JS. I'm getting the attribute value as "1,2..", What I need is "one,two.. when I change the select tag.

我的示例代码是

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>

这是小提琴.

推荐答案

尝试这个

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

这篇关于无法使用Angular JS获得选定的选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆