ng-model in ng-repeat;获得ng-model的价值

时间:2015-05-30 04:56:35

标签: javascript angularjs ng-repeat angularjs-ng-model

我有以下奇怪的情况 - 控制器代码段 -

$scope.filterNum = {0: 'filterVal1', 1: 'filterVal2', 2: 'filterVal3', 3: 'filterVal4', 4: 'filterVal5', 5: 'filterVal6',
                    6: 'filterVal7', 7: 'filterVal8', 8: 'filterVal9', 9: 'filterVal10', 10: 'filterVal11', 11: 'filterVal12',
                    12: 'filterVal13', 13: 'filterVal14', 14: 'filterVal15', 15: 'filterVal16', 16: 'filterVal17', 17: 'filterVal18'};

$scope.operatorNum = {0: 'operatorVal1', 1: 'operatorVal2', 2: 'operatorVal3', 3: 'operatorVal4', 4: 'operatorVal5', 5: 'operatorVal6',
                    6: 'operatorVal7', 7: 'operatorVal8', 8: 'operatorVal9', 9: 'operatorVal10', 10: 'operatorVal11', 11: 'operatorVal12',
                    12: 'operatorVal13', 13: 'operatorVal14', 14: 'operatorVal15', 15: 'operatorVal16', 16: 'operatorVal17', 17: 'operatorVal18'};
$scope.getNumber = function(num) {
    return new Array(num);   
};

HTML代码段

 <div class="row" ng-repeat="i in getNumber(18) track by $index">
        <div class="col-md-3">
        <select class="form-control" ng-model="filterNum[$index]">
            <option ng-repeat="filter in filters" value="{{filter}}">{{filter}}</option>                
        </select>
        </div>                                  
        Hello {{filterNum[$index].value}}
    </div>              

所以我想要的是打印Hello (value selected in select control)但是当前表达式什么都不打印。

我已经提到了这个问题this question

1 个答案:

答案 0 :(得分:3)

使用{{ filterNum[$index] }}代替{{filterNum[$index].value}}

并且,我使用filters变量的虚拟值。

请参阅以下代码。

&#13;
&#13;
var app = angular.module('app', []);


app.controller('ctrl', function($scope) {
  $scope.filterNum = {
    0: 'filterVal1',
    1: 'filterVal2',
    2: 'filterVal3',
    3: 'filterVal4',
    4: 'filterVal5',
    5: 'filterVal6',
    6: 'filterVal7',
    7: 'filterVal8',
    8: 'filterVal9',
    9: 'filterVal10',
    10: 'filterVal11',
    11: 'filterVal12',
    12: 'filterVal13',
    13: 'filterVal14',
    14: 'filterVal15',
    15: 'filterVal16',
    16: 'filterVal17',
    17: 'filterVal18'
  };

  $scope.operatorNum = {
    0: 'operatorVal1',
    1: 'operatorVal2',
    2: 'operatorVal3',
    3: 'operatorVal4',
    4: 'operatorVal5',
    5: 'operatorVal6',
    6: 'operatorVal7',
    7: 'operatorVal8',
    8: 'operatorVal9',
    9: 'operatorVal10',
    10: 'operatorVal11',
    11: 'operatorVal12',
    12: 'operatorVal13',
    13: 'operatorVal14',
    14: 'operatorVal15',
    15: 'operatorVal16',
    16: 'operatorVal17',
    17: 'operatorVal18'
  };


  $scope.filters = [
    "filterVal1",
    "filterVal2"
  ];


  $scope.getNumber = function(num) {
    return new Array(num);
  };

});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
  <div class="row" ng-repeat="i in getNumber(18) track by $index">
    <div class="col-md-3">
      <select class="form-control" ng-model="filterNum[$index]">
        <option ng-repeat="filter in filters" value="{{filter}}">{{filter}}</option>
      </select>
    </div>Hello {{filterNum[$index]}}</div>
</div>
&#13;
&#13;
&#13;