@adriancarriger's answer is great, but a small improvement can be made. Since you're pushing the new elements to the bottom of the list, I guess that it would be nice to always scroll the content to the bottom, to make the new items always visible. That could be easily achieved by adding just a few lines of code:
// First add ViewChild and Content imports
import { ViewChild, Component, ViewEncapsulation } from '@angular/core';
import { NavController, Content } from 'ionic-angular';
@Component({
selector: 'page-home',
templateUrl: 'pages/home/home.html',
styles: [`
.scroll-content {
display: flex;
flex-direction: column;
}
.scroll-content ion-list {
margin-top: auto;
margin-bottom: 0;
}
`],
encapsulation: ViewEncapsulation.None
})
export class HomePage {
@ViewChild(Content) content: Content; // <-- get a reference of the content
count = 4; // for demo
appName = 'Ionic App';
array = [
{ name: 'item-1' },
{ name: 'item-2' },
{ name: 'item-3' }
];
constructor(private navController: NavController) {
}
search() {
console.log('searching...');
}
add(number) {
let itemNumber = this.count;
this.array.push({ name: 'item-' + itemNumber });
this.scrollToBottom(); // <- when the new item is pushed, scroll to the bottom to show it
this.count += number;
}
scrollToBottom(){
// use the content's dimension to obtain the current height of the scroll
let dimension = this.content.getContentDimensions();
// scroll to it (you can also set the duration in ms by passing a third parameter to the scrollTo(x,y,duration) method.
this.content.scrollTo(0, dimension.scrollHeight);
}
}