Using select value in ng-repeat

Right now I have to use the limitTo filter with the selected value, but I really want to use the selected value as the number of times the ng-repeat should run. But this value is an object, and ng-repeat expects an array.

So in theory it works. But ultimately all the information should generate this table.

Update: the code works, but instead of declaring a range [1,2,3,4,5,6]

(and applying a filter afterwards), I want to use a value factorSelection.value

that is an object while it expects an array.

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>
      

Run codeHide result


+3


source to share


1 answer


Got a response via another channel (Google+), will add it as a response with updated code.

Added functions to get the length and return these values ​​as parameters.

This still ends up in conflict with various input fields, but this is something I can work on to fix and update once when done.



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

.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 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>
  
      

Run codeHide result


0


source







All Articles