I have a scenario in application, where i have to run the loop (ng-repeat) no of times based on a parameter set. For Example:-
$scope.rating = 3;
then run the loop (using ng-repeat) for 3 times.
I have a scenario in application, where i have to run the loop (ng-repeat) no of times based on a parameter set. For Example:-
$scope.rating = 3;
then run the loop (using ng-repeat) for 3 times.
Below will the code in your controller..
$scope.rating = 3;
$scope.getRating = function(rate) {
return new Array(rate);
}
Below will be your template.
<div ng-repeat="i in getRating(rating) track by $index">
<input type="text" name="txt" value="{{$index}}"/>
</div>
I think this is cleaner and shorter way
var app = angular.module('app', []);
app.controller('Ctrl', function($scope) {
$scope.counter = 2;
$scope.generateFields = function(count) {
return new Array(count);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="Ctrl">
Insert Number To Generate Fields:
<input type='number' ng-model='counter'/>
<hr>
<div ng-repeat="i in generateFields(counter) track by $index">
<input type="text" name="txt" value="Input Field {{$index}}"/>
</div>
</div>
You can do it by nested ng-repeat using like this :
In controller
var rating = [3];
//u can set it dynamically too {var rating = [req.body.rating]}
//erq.body.rating is getting value from text/input box
$scope.rating = rating;
In HTML
<div ng-repeat = i in rating>
<div ng-repeat = ____></div> // this ng-repeat will run based on rating value
</div>