Using Angular2 ngFor index

后端 未结 3 638
再見小時候
再見小時候 2021-02-05 02:52

I have this code:

3条回答
  •  执念已碎
    2021-02-05 03:07

    For index try this:

    Controller File add function :

    chunks(array, size) {
      let results = [];
      while (array.length) {
        results.push(array.splice(0, size));
      }
      return results;
    };
    

    In you view file :

    This will work for all number , not only %3 numbers.

    @Teddy Sterne's solution will work incase of the number is %3 If we have 8 products it will show only 6 last 2 will be lost , in this it will also be shown.

    And it will create extra blank div tags for not %3 index , if you inspect the element and check , because it will loop through each product and div will get repeated no matter if its index %3 or not.

提交回复
热议问题