【问题标题】:unable to bind value to ng-model of select with ng-repeat on button click无法使用 ng-repeat 在按钮单击时将值绑定到选择的 ng-model
【发布时间】:2017-08-16 00:33:14
【问题描述】:

我有如下的 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;
        }
    });
};

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

【问题讨论】:

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


    【解决方案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;
                }
            });
        };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-24
      • 2015-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-12
      • 1970-01-01
      相关资源
      最近更新 更多