question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

πŸš€ 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:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Airbladercommented, Sep 20, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found