[英]How to dynamically generate ng-option in angularjs
Here, from the json it will show the quantity. 在这里,从json它将显示数量。 Based on quantity value, select option needs to generate dynamically as below.
根据数量值,选择选项需要动态生成,如下所示。
you can use limitTo
with ng-options
to dynamically show options
of select
element 您可以将
limitTo
与ng-options
一起使用以动态显示select
元素的options
Wish I have got what you want. 希望我能得到你想要的。
angular.module("app", []).controller("myCtrl", function($scope) { $scope.selectedItem = 1; $scope.list = [1, 2, 3, 4]; $scope.quantity = 1; });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="app" ng-controller="myCtrl"> <select ng-model="quantity" ng-options="value as value for value in list"></select> <br> <select class="classname" ng-options="value as value for value in list | limitTo:quantity:0" ng-model="selectedItem"> <!--<option ng-repeat="item in list | limitTo:quantity:0">{{item}}</option> --> </select> {{selectedItem}} </div>
As for mu understanding of the question you can create a custom filter to filter out options based on the quantity. 至于亩对问题的理解,您可以创建一个自定义过滤器以根据数量过滤出选项。
angular.module("app",[]) .controller("ctrl",function($scope){ $scope.obj = { "list": [ { "quantity": [ 2, 3, 4 ]} ]} }) .filter('cust',function(){ return function(item){ var arr = [] for(var i =1; i<= parseInt(item); i++){ arr.push(i) } return arr } })
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="app" ng-controller="ctrl"> <div ng-repeat="q in obj.list"> <div ng-repeat="i in q.quantity"> <select ng-init="q.selectval[i] = i" class="classname" ng-model="q.selectval[i]" ng-options="value as value for (key,value) in i | cust" > </select> {{q.selectval[i]}} </div> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.