【问题标题】:How to get selected items id in select option in angular js如何在角度js的选择选项中获取选定项目的ID
【发布时间】:2015-03-22 05:57:44
【问题描述】:

在下面的代码中,更新函数没有得到正确的 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>

【问题讨论】:

  • selectedCategory 传递给您的update 函数

标签: javascript angularjs select


【解决方案1】:

由于您已经在视图中选择了类别,只需执行此操作

<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

【讨论】:

  • alert (category.id) 在更新函数中给出未定义
  • 你看到被通过的类别了吗?
【解决方案2】:

selectedCategory 可以在 update 函数中访问:

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

【讨论】:

    【解决方案3】:

    试试这样 样本数据

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

    在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>
    

    【讨论】:

      猜你喜欢
      • 2020-12-25
      • 2015-12-05
      • 2017-11-28
      • 2012-10-19
      • 1970-01-01
      • 2019-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多