无法在按钮单击时使用ng-repeat将值绑定到选择的ng-model

时间:2017-03-23 12:38:16

标签: angularjs select angularjs-ng-repeat angular-ngmodel

我有TableObject,如下所示

$scope.tableObject = [
    {
        'Column_Name' : 'Dummy 1',
        'Column_Cliass' : 'Valid'
    },
    {
        'Column_Name' : 'Dummy 2',
        'Column_Cliass' : 'Invalid'
    }
]

现在使用ng-repeat绘制选择控件,如下所示。

<div class="row networkDataBR" ng-repeat="lTable in tableObject">
    <select class="form-control" ng-model="lookupFile" ng-options="option as option.name for option in onlyFiles">
    </select>   
</div>

它将生成两个选择控件,现在开启按钮单击我想为控制器中的一个选择控件指定值。

<input class="btn btn-primary" type="button" ng-click="appendFile("Network")" value="Append" />

$scope.appendFile = function(dataValue) {
    _.each($scope.tableObject, function (lTable) {
        if (lTable.Column_Name === "dummy") {
            $scope.lookupFile = dataValue;
        }
    });
};

但是,它适用于选择控件。那么如何申请单选?

1 个答案:

答案 0 :(得分:1)

您对两个选择控件使用相同的模型变量。使用像这样的不同变量

<div class="row networkDataBR" ng-repeat="lTable in tableObject">
    <select class="form-control" ng-model="lookupFile[$index]" ng-options="option as option.name for option in onlyFiles">
    </select>   
</div>

<input class="btn btn-primary" type="button" ng-click="appendFile("Network",0)" value="Append" />


$scope.lookupFile={};
    $scope.appendFile = function(dataValue,index) {
        _.each($scope.tableObject, function (lTable) {
            if (lTable.Column_Name === "dummy") {
                $scope.lookupFile[index] = dataValue;
            }
        });
    };