Angularjs将ng-model索引数据提交给控制器

时间:2016-02-17 02:43:44

标签: angularjs select indexing ng-repeat

我是angularjs的新手,并且使用ng-repeat生成了多个选项,如下所示......

                                            <table>
                        <tr ng-repeat="(key, value) in paramMapByCmd">
                        <td></td>
                        <td><input  type="hidden" ng-model="myData.param" value="{{key}}" /><label>{{key}}:</label></td>
        <!--                <td><input type="text" ng-model="myData.param[i]" /></td> -->

                        <td>
                            <select name="param_val_sel" ng-options="item as item for item in value track by $index" 
                                    ng-model="myData.paramValue[$index]"
                                    ng-change="changeParamValue()">
                                <option value="">Select Parameter Value</option>
                            </select>
                        </td>               
                    </tr>    
            </table>

现在当表单被提交时,如何在控制器中检索数组(希望它是一个数组?)?

$scope.submitCmd = function() {

    var comData = $scope.myData.command;
    use a loop here....
    ????var paramData = $scope.tl1Data.paramValue[$index]; ????

你是怎么做到的?

P.S。我添加了更多html来显示ng-repeat ...

2 个答案:

答案 0 :(得分:1)

以下是ng-options

的简单示例

<强>控制器

app.controller('MainCtrl', function($scope) {

  $scope.items = [];
  $scope.selectedItem = {};
  $scope.items = [{name: 'one', id: 30 },{ name: 'two', id: 27 },{ name: 'threex', id: 50 }];
});

查看

<body ng-controller="MainCtrl">
  <p>selected item is : {{selectedItem}}</p>

  <p> name of selected item is : {{selectedItem.name}} </p>

  <select ng-model="selectedItem" ng-options="item.name for item in items track by item.id"></select>
</body>

在这种情况下,您选择的结果将位于$scope.selectedItem。这意味着您的结果应该是您放入ng-model

的变量

Here is the plunker with example

在您的情况下,$index ng-model中的ng-model="tl1Data.paramValue"不需要这样console.log($scope.tl1Data.paramValue),然后在控制器中检查您的范围 比如'use strict'; var electron = require('electron'); var app = electron.app; var BrowserWindow = require('browser-window'); app.on('ready', function () { var mainWindow = new BrowserWindow ({ width: 800, height: 600, webPreferences: { webSecurity: false } }); mainWindow.loadURL('https://example.com/'); mainWindow.webContents.openDevTools(); });

答案 1 :(得分:0)

在您的控制器$scope.tl1Data.paramValue中已经存储了一个数组。 要迭代它,你可以做这样的事情:

for(item in $scope.tl1Data.paramValue) {
    console.log(item);
}