ngRepeat
See original GitHub issueπ feature request
create a ngRepeat Structural Directive
Description
create a directive to explicit indicate the number of time an element have to repeat. Instead of create an array of elements to only make a ngFor
Describe the solution youβd like
If you have a solution in mind, please describe it.@Directive({
selector: '[ngRepeat]'
})
export class RepeatDirective implements OnChanges {
private quantity: number;
@Input()
public set ngRepeat(value: number) {
this.quantity = value;
}
public constructor(private templateRef: TemplateRef<any>, private viewContainer: ViewContainerRef) {
}
public ngOnChanges() {
const length = this.viewContainer.length;
for (let x = length; x < this.quantity; x++) {
this.viewContainer.createEmbeddedView(this.templateRef, { index: x, onRemoveAt: this.onRemoveAt(this.viewContainer) }, x);
}
}
/**
* expose a hook
*
* @param viewContainer container ref
*/
private onRemoveAt(viewContainer: ViewContainerRef) {
return (index) => {
viewContainer.remove(index);
// update index
for (let x = 0; x < viewContainer.length; x++) {
const view = viewContainer.get(x) as EmbeddedViewRef<any>;
viewContainer.move(view, x);
view.context.index = x;
}
};
}
}
Usage
simple usage
<ng-container *ngRepeat="5, let i = index">
<div>element NΒ° {{i}}</div>
</ng-container>
with delete
<ng-container *ngRepeat="quantity, let i = index; let onRemove = onRemoveAt">
<div>element NΒ° {{i}}</div>
<button (click)="onRemove(i); quantity -= 1">Delete</button>
</ng-container>
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
ngRepeat - AngularJS: API
The ngRepeat directive instantiates a template once per item from a collection. Each template instance gets its own scope, where the given loop...
Read more >Angular ng-repeat Directive - W3Schools
The ng-repeat directive repeats a set of HTML, a given number of times. The set of HTML will be repeated once per item...
Read more >Understanding the ngRepeat 'track by' expression
This feature allows you to associate a JavaScript object with an ngRepeat DOM (Document Object Model) node using a unique identifier. With thisΒ ......
Read more >Angular-JS ng-repeat Directive - GeeksforGeeks
Angular-JS ng-repeat directive is a handy tool to repeat a set of HTML code for a number of times or once per item...
Read more >AngularJS ng-repeat Directive with Example - Guru99
The ng-repeat directive in AngularJS is used to display repeating values defined in the controller. Sometimes we require displaying a list ofΒ ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
https://www.npmjs.com/package/vstep-ng-repeat
My library ngx-structurals also has this. I found this mostly useful for prototyping and building skeleton screens. Beyond that Iβve never had the need for this.