简体   繁体   English

如何在Angular JS的选择选项中获取选定的项目ID

[英]How to get selected items id in select option in angular js

In the following code update function is not getting the proper ID passed it. 在以下代码中,更新功能未获得正确的ID。

How to get the proper is in angularJS for selected option. 如何获得正确的选项是在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>

Since you already have selectedCategory in your view, just do this 由于您已经在视图中选择了类别,因此只需执行此操作

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

Here is the working plnker 这是工作plnker

selectedCategory can be accessed inside update function: 可以在更新函数中访问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
};

try like this sample data 尝试像这样的样本数据

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

in html code 在html代码中

<select name="" id="" class="form-control form-custome" 
    ng-options="option.name for option in selectCustName.availableOptions track by option.id"
    ng-model="selectCustName.selectedOption">
</select>

<p>your selected {{ selectCustName.selectedOption }}</p>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM