如何获取使用循环中的ng选项实现的Select框的值

How to get the value of a Select box implemented using ng-options which is inside a loop?

本文关键字:选项 ng 实现 Select 循环 何获取 获取      更新时间:2023-09-26

我有一个显示客户信息的列表页面,每行客户中都有一个选择框,其中包含一些选项,如编辑、取消、删除等。客户使用ng repeat指令显示,具有上述选项的选择框使用ng选项实现。

我试图从下拉列表中获取每行所选项的值,但它不起作用,当我将选择框代码片段粘贴到循环外时,代码起作用了。

这是我显示客户的代码

  <table class="table display table-striped table-bordered" ng-table="tableParams">
    <tr ng-repeat="customer in $data | filter: search | filter :name">
       <td data-title="'Reseller ID'" sortable="'ResellerId'" > 
            {{customer.ResellerId}}
        </td>
        <td data-title="'Reseller Name'" sortable="'ResellerName'">
            {{customer.ResellerName}}
        </td>
        <td data-title="'Customer ID'" sortable="'CustomerID'">
            {{customer.CustomerID}}
        </td>
        <td data-title="'Customer Name'" sortable="'CustomerName'">
            {{customer.CustomerName}}
        </td>
        <td data-title="'Status'" sortable="'Status'">
            {{customer.Status}}
        </td>
        <td data-title="'Available Funds'" sortable="'AvailableFunds'">
            {{customer.AvailableFunds}}
        </td>
        <td data-title="'Created Date'" sortable="'CreatedDate'">
            {{customer.CreatedDate}}
        </td>
        <td>
        <form name="statusForm">
        <select class="form-control status" ng-model="actionslist"  name="actions" ng-options="action.name for action in action_options" ng-change="editCustomer()">
            <option value="">Select Action</option>
        </select>
        </form>
        </td>
        <td><a href="#" class="btn btn-info" data-toggle="tooltip" data-original-title="View Domains"><i class="fa fa-desktop"></i></a> <a href="#" class="btn btn-warning" data-toggle="tooltip" data-original-title="View Subscriptions"><i class="fa fa-rss"></i></a> <a href="#" class="btn btn-success" data-toggle="tooltip" data-original-title="Login"><i class="fa fa-sign-in"></i></a> </td>
    </tr>
  </table>

这是我的角度代码

var CustomerController = function ($scope, $filter, $http, ngTableParams) {
    $scope.action_options = [{
        name: 'Edit',
        value: 'edit'
    }, {
        name: 'Suspend',
        value: 'suspend'
    }, {
        name: 'Cancel',
        value: 'cancel'
    }, {
        name: 'Delete',
        value: 'delete'
    }, {
        name: 'Reset Password',
        value: 'reset'
    }];
    $scope.editCustomer = function () {
        //$scope.itemList=[];
        alert($scope.item.value);
        //alert($scope.selected.actions);
    }
}

有人能告诉我我在这里犯了什么错误吗?

它将通过您的ng型号保存

$scope.editCustomer = function () {
    alert($scope.actionslist);
}

工作小提琴

试试这个:

<select ng-model="action" ng-options="obj.value as obj.name for obj in action_options"></select>

看看这个答案https://stackoverflow.com/a/12140310/1530725