如何在角度js中的选择选项中获取所选项目ID

时间:2015-03-22 05:57:44

标签: javascript angularjs select

在下面的代码中,更新函数没有获得传递它的正确ID。

如何选择适合选项的angularJS。

<select 
    ng-model="selectedCategory" 
    size="10" 
    ng-options="category.id as category.name for category in Categories | orderBy:'name'" 
    ng-change="update(Categories[selectedCategory-1].id)">
</select>

3 个答案:

答案 0 :(得分:0)

由于您的视图中已经有selectedCategory,所以只需执行此操作

<select 
    ng-model="selectedCategory" 
    size="10" 
    ng-options="category.id as category.name for category in Categories | orderBy:'name'" 
    ng-change="update(selectedCategory)">
</select>

 $scope.update= function (category) {
       $scope.CategoryID = category.id;
    };

这是工作plnker

答案 1 :(得分:0)

可以在 update 函数中访问

selectedCategory

<select 
    ng-model="selectedCategory" 
    size="10" 
    ng-options="category.id as category.name for category in Categories | orderBy:'name'" 
    ng-change="update(selectedCategory)">
</select>

$scope.update= function () {
    console.log($scope.selectedCategory); //this is selected cotegory id
};

答案 2 :(得分:0)

试试这样 样本数据

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<form>
  <select name="country" id="country" class="select">
    <option value="">Select</option>
    <option value="1">india</option>
    <option value="2">America</option>
  </select>
  <select name="currency" id="disable" class="select">
    <option value="">Select</option>
    <option value="1">INR</option>
    <option value="2">DOLLAR</option>
  </select>
  <br>
  <br>
</form>

在html代码中

$scope.selectCustName = {
        availableOptions: [
                           {id: '0', name: 'All'},
                           {id: '1', name: 'Option A'},
                           {id: '2', name: 'Option B'},
                           {id: '3', name: 'Option C'}
                         ],
       selectedOption: {id: '0', name: 'All'} //This sets the default value of the select in the ui
};