angular.js - How to get the selected data column in angular select
大家讲道理
大家讲道理 2017-05-15 17:11:43
0
2
564
<p>选项,{{selected}}</p>
<select ng-model="selected" ng-options="item.id as item.name for item in selectData">
        <option value="">-- 请选择 --</option>
</select>
$scope.selected = 1004;
$scope.selectData = [
    {id:1001,name:"零零一"},
    {id:1002,name:"零零二"},
    {id:1003,name:"零零三"},
    {id:1004,name:"零零四"}
];

How to get the data of the selected column. selected can only get the id value, but I want the value of this column. And it does not interfere with the default selection of an item through the id value. For example, if I select this item through id:1004, I not only want the id value but also all the items in this column.

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(2)
我想大声告诉你

Don’t you think there is a contradiction in your needs? You assign an id value to ng-model, but you also expect the value of ng-model to be an object?

If you want the final ng-model to be a column, but you can only give the id value of a selected item (which should be what you encountered during page editing), you can first filter out the corresponding item by giving the id and assign it to selected. And the ng-options expression needs to be changed to item as item.name for item in selectData track by item.id

$scope.selectData = [
  {id:1001,name:"零零一"},
  {id:1002,name:"零零二"},
  {id:1003,name:"零零三"},
  {id:1004,name:"零零四"}
];
$scope.selected = $scope.selectData.filter(item => item.id === 1004)[0];
<select ng-model="selected" ng-options="item as item.name for item in selectData track by item.id">
    <option value="">-- 请选择 --</option>
</select>
过去多啦不再A梦
<p>选项,{{selected}}</p>
<select ng-model="selected" ng-options="item.name for item in selectData">
        <option value="">-- 请选择 --</option>
</select>
$scope.selected = $scope.selectData[3];
$scope.selectData = [
    {id:1001,name:"零零一"},
    {id:1002,name:"零零二"},
    {id:1003,name:"零零三"},
    {id:1004,name:"零零四"}
];
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template