在ng-repeat

时间:2015-05-27 13:17:00

标签: javascript angularjs angularjs-ng-repeat

现在我必须使用带有所选值的limitTo过滤器,但我真正想要的是使用所选值作为ng-repeat应该运行的次数。但是该值是一个对象,而ng-repeat需要一个数组。

所以在理论上,这是有效的。但最终所有信息都需要生成该表。

更新:代码有效,但不必自己声明范围[1,2,3,4,5,6](之后应用过滤器)我想使用值factorSelection.value,它是一个对象,而它需要一个数组。



var app = angular.module('application', []);

app.controller('mainCtrl', ['$scope', function($scope) {
  $scope.factors = [
    {value: '1', name: '1 factor'},
    {value: '2', name: '2 factors'},
    {value: '3', name: '3 factors'},
    {value: '4', name: '4 factors'},
    {value: '5', name: '5 factors'}
  ];
  
  $scope.states = [
    {value: '1', name: '1 state'},
    {value: '2', name: '2 states'},
    {value: '3', name: '3 states'},
    {value: '4', name: '4 states'},
    {value: '5', name: '5 states'}   
  ];
}]);

.item {
  padding: 10px;
  border-bottom: 1px solid #999;
}

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="application">

  <div ng-controller="mainCtrl">
   <select ng-model="factorSelection" ng-options="factor.name for factor in factors">
     <option value="">-- choose amount of factors --</option>
   </select>    
    
    <div ng-repeat="factor in [1,2,3,4,5,6] | limitTo: factorSelection.value">
      <div class="item">
        factor {{ factor }}
        <select ng-model="stateSelection" ng-options="state.name for state in states">
          <option value="">-- choose amount of states --</option>
        </select>
        
        <div ng-repeat="state in [1,2,3,4,5,6,7,8,9,10] | limitTo: stateSelection.value">
          <input type="text" ng-model="state" placeholder="" />
          {{ state }}
        </div>
      </div>
    </div>
   <table>
      <thead>
        <tr>
          <th ng-repeat="factor in [1,2,3,4,5,6] | limitTo: factorSelection.value"> {{ factor }}</th>
        </tr>
      </thead>
     
     <tbody>
       <tr ng-repeat="factor in [1,2,3,4,5,6] | limitTo: factorSelection.value">
         <td ng-repeat="factor in [1,2,3,4,5,6] | limitTo: factorSelection.value"> 
           td {{ factor }}
         </td>
       </tr>
     </tbody>
    </table>
  </div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

通过其他渠道(Google+)获得答案,将其添加为更新代码的答案。

添加了获取长度并将这些值作为选项返回的函数。

这仍然会导致与不同的输入字段发生冲突,但是一旦完成,我就可以继续修复和更新。

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

app.controller('mainCtrl', ['$scope', function($scope) {
  $scope.factors = [
    {value: '1', name: '1 factor'},
    {value: '2', name: '2 factors'},
    {value: '3', name: '3 factors'},
    {value: '4', name: '4 factors'},
    {value: '5', name: '5 factors'}
  ];
  
  $scope.states = [
    {value: '1', name: '1 state'},
    {value: '2', name: '2 states'},
    {value: '3', name: '3 states'},
    {value: '4', name: '4 states'},
    {value: '5', name: '5 states'}   
  ];
  
  /*
  * Get factor options 
  */
  $scope.getFactors = function(len){
   return $scope.getOptions(len, $scope.factors);
  }
  /*
  * Get state options
  */
  $scope.getStates = function(len){
    return $scope.getOptions(len, $scope.states);
  }
  
  /*
  * 
  */
  $scope.getOptions = function(len, options){
    if (len && len <= options.length ){
       return options.slice(0, len);
    }
  }
}]);
&#13;
.item {
  padding: 10px;
  border-bottom: 1px solid #999;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="application">

  <div ng-controller="mainCtrl">
   <select ng-model="factorSelection" ng-options="factor.name for factor in factors">
     <option value="">-- choose amount of factors --</option>
   </select>    
    
    <div ng-repeat="factor in getFactors(factorSelection.value)">
      <div class="item">
        factor {{ factor.value }}
        <select ng-model="stateSelection" ng-options="state.name for state in states">
          <option value="">-- choose amount of states --</option>
        </select>
        
        <div ng-repeat="state in getStates(stateSelection.value)" >
          <input type="text" ng-model="state.value" placeholder="" />
          {{ state.value }}
        </div>
      </div>
    </div>
   <table>
      <thead>
        <tr>
          <th ng-repeat="factor in [1,2,3,4,5,6] | limitTo: factorSelection.value"> {{ factor }}</th>
        </tr>
      </thead>
     
     <tbody>
       <tr ng-repeat="factor in [1,2,3,4,5,6] | limitTo: factorSelection.value">
         <td ng-repeat="factor in [1,2,3,4,5,6] | limitTo: factorSelection.value"> 
           td {{ factor }}
         </td>
       </tr>
     </tbody>
    </table>
  </div>
  
&#13;
&#13;
&#13;