繁体   English   中英

在ng-repeat中使用选择值

[英]Using select value in 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> 

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

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

这仍然会与不同的输入字段产生一些冲突,但这是我可以在完成后修复和更新的工作。

 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> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM