Show More Button in Angular

Have you ever have a task with large lists?
Sometimes we need to hide part of items list and show it when user will want. For example, it could be online shops, blogs, chats, forums or something else.
Let me show you the simplest way to create “show more” button in Angular app.
I use angular native SlicePipe, which transform your data and show the number of items what you want.
Let’s create list of posts.
In app.component.ts I create an array with posts object. Also, I set number of items which would be shown.
// app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
})
export class AppComponent {
public showCountOfPost = 2;
public posts = [
{title: 'post1'},
{title: 'post2'},
{title: 'post3'},
{title: 'post4'},
{title: 'post5'},
{title: 'post6'},
{title: 'post7'},
{title: 'post8'},
]
showMore(){
this.showCountOfPost += 2;
}In app.component.html template I use *ngFor directive to render my posts. Than I add button and after click on it your lists of items will be increased.
// app.component.html
<div *ngFor="let post of posts | slice:0:showCountOfPost; let i=index">
<h3>{{post.title}}</h3>
</div>
<button *ngIf="showCountOfPost < posts.length" (click)="showMore()">Show more</button>That’s all from me. Thanks for reading.