2015-05-30 78 views
2

的價值我有以下怪異情況 - 控制器片段 -ng-repeat中的ng模型;得到NG-模型

$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

嘗試'{{filterNum [$指數]}}' –

+0

你在哪裏的 '過濾器'?一切都取決於它,你可以使用[ng-options](https://docs.angularjs.org/api/ng/directive/ngOptions)而不是選項標籤 –

回答

3

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

和,我已經使用虛擬值爲filters變量。

請參閱下面的代碼。

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); 
 
    }; 
 

 
});
<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>

+0

謝謝,它工作:) – codeomnitrix