AngularJS builders frequently brush conditions requiring repetitive HTML components with out iterating complete an current information array. However tin you effectively usage ng-repetition
to accomplish this successful AngularJS with out resorting to creating dummy arrays? This station dives into respective elegant options for repeating components a circumstantial figure of instances utilizing ng-repetition
, offering broad examples and champion practices for optimizing your AngularJS codification.
Creating a Customized Filter for Figure Iteration
1 of the about versatile approaches is creating a customized filter. This permits you to make a series of numbers connected the alert, which ng-repetition
tin past iterate complete.
Present’s however to instrumentality specified a filter:
javascript angular.module(‘myApp’, []).filter(‘scope’, relation() { instrument relation(enter) { var lowBound, highBound; control (enter.dimension) { lawsuit 1: lowBound = zero; highBound = parseInt(enter[zero]) - 1; interruption; lawsuit 2: lowBound = parseInt(enter[zero]); highBound = parseInt(enter[1]); interruption; default: instrument enter; } var consequence = []; for (var i = lowBound; i <= highBound; i++) { result.push(i); } return result; }; }); This filter permits you to specify the scope straight successful your HTML:
html
A less complicated, but effectual methodology entails creating an bare array of the desired dimension utilizing the fresh Array(n)
constructor and leveraging path by $scale
inside your ng-repetition
directive. This attack avoids the demand for a customized filter.
html
Leveraging a Controller Relation
Different attack includes defining a relation inside your controller that returns an array of the desired dimension. This technique offers a broad separation of issues and tin beryllium utile for much analyzable logic.
javascript angular.module(‘myApp’, []).controller(‘MyCtrl’, relation($range) { $range.getNumber = relation(num) { instrument fresh Array(num); }; }); html
Running with Server-Broadside Information
If the figure of repetitions relies upon connected information fetched from a server, you tin easy combine these methods inside your controller last retrieving the information.
Illustration: Ideate you have a number from an API endpoint:
javascript $http.acquire(’/api/itemCount’).past(relation(consequence) { $range.itemCount = consequence.information.number; }); Past successful your HTML:
html
Infographic Placeholder: Ocular examination of the antithetic strategies.
Knowing the Show Implications
Piece each strategies accomplish the desired consequence, knowing their show implications tin beryllium important for bigger functions. Utilizing fresh Array(n)
with path by $scale
is mostly the about performant, arsenic it avoids the overhead of relation calls oregon filter execution. Nevertheless, for dynamic scope procreation primarily based connected analyzable logic, a controller relation oregon customized filter mightiness beryllium much appropriate. See profiling your exertion to find the champion attack for your circumstantial script.
- Place the origin of the repetition number (static figure, server information, and so forth.).
- Take the due technique (filter, controller relation, oregon
fresh Array(n)
). - Instrumentality the chosen technique successful your codification.
- Trial totally to guarantee accurate performance.
These strategies supply almighty and versatile options for repeating HTML components with ng-repetition
with out needing pre-current arrays. Selecting the correct technique volition be connected the circumstantial wants of your task. By knowing the nuances of all attack, you tin compose cleaner, much businesslike AngularJS codification. For additional speechmaking connected AngularJS champion practices, research assets similar AngularJS authoritative documentation and ngRepeat documentation. You mightiness besides discovery worth successful this article connected AngularJS show tuning.
By implementing these methods, you tin optimize your AngularJS functions and make much dynamic person experiences. Retrieve to take the technique champion suited for your task’s complexity and show necessities. Research the supplied sources for much successful-extent cognition and proceed experimenting to maestro the powerfulness of ng-repetition
successful AngularJS improvement. Cheque retired our weblog for much AngularJS suggestions and tips.
FAQ:
Q: Wherefore is path by $scale
crucial?
A: It prevents errors precipitated by duplicate keys successful the generated array, particularly once utilizing fresh Array(n)
.
Question & Answer :
Is location a manner to ng-repetition
a outlined figure of occasions alternatively of ever having to iterate complete an array?
For illustration, beneath I privation the database point to entertainment ahead 5 occasions assuming $range.figure
close to 5 successful summation incrementing the figure truthful all database point increments similar 1, 2, three, four, 5
Desired consequence:
<ul> <li><span>1</span></li> <li><span>2</span></li> <li><span>three</span></li> <li><span>four</span></li> <li><span>5</span></li> </ul>
Replace (9/25/2018)
Newer variations of AngularJS (>= 1.three.zero) let you to bash this with lone a adaptable (nary relation wanted):
<li ng-repetition="x successful [].constructor(figure) path by $scale"> <span>{{ $scale+1 }}</span> </li>
$range.figure = 5;
This was not imaginable astatine the clip the motion was archetypal requested. Recognition to @Nikhil Nambiar from his reply beneath for this replace
First (5/29/2013)
Astatine the minute, ng-repetition
lone accepts a postulation arsenic a parameter, however you may bash this:
<li ng-repetition="i successful getNumber(figure)"> <span>{{ $scale+1 }}</span> </li>
And location successful your controller:
$range.figure = 5; $range.getNumber = relation(num) { instrument fresh Array(num); }
This would let you to alteration $range.figure
to immoderate figure arsenic you delight and inactive keep the binding you’re trying for.
EDIT (1/6/2014) – Newer variations of AngularJS (>= 1.1.5) necessitate path by $scale
:
<li ng-repetition="i successful getNumber(figure) path by $scale"> <span>{{ $scale+1 }}</span> </li>
Present is a fiddle with a mates of lists utilizing the aforesaid getNumber
relation.