Reputation: 2535
i have a message conversation section, where in i need to show the scroll to the bottom, and when the page opens again the scroll must be at the bottom. i mean last message should be displayed first.
HTML:
<ul>
<li *ngFor="let reply of message_show.messages">
<img [src]="reply.from_user_image || '../assets/images/msg.png'"/>
<p><b>{{reply.name}} </b> <span> {{reply.updated_at | date:'dd.MM.yyyy'}} - {{reply.updated_at | date:'h:mm'}}</span></p>
<p>{{reply.text}}</p>
</li>
</ul>
Ts:
loadMessages() {
this.service
.getMessages()
.subscribe(
data => {
this.messagesdata = data;
this.activeMessages = data.filter(msg => msg.active == true && msg.from_user_name !== 'Anonymus' && msg.messages.length > 0)
this.closedMessages = data.filter(msg => msg.active == false && msg.from_user_name !== 'Anonymus' && msg.messages.length > 0);
if (this.activeMessages.length > 0) {
if(!this.message_show) {
var test = this.message_show = this.activeMessages[0];
this.message = true;
this.message_id = this.activeMessages[0].id;
this.message_show.messages.map(function(msg) {
if(msg.from_user_id == test.from_user_id) {
msg.from_user_image = test.from_user_image;
} else {
msg.from_user_image = test.to_user_image;
}
if(msg.to_user_id == test.to_user_id) {
msg.to_user_image = test.to_user_image;
} else {
msg.to_user_image = test.to_user_image;
}
})
}
}
},error => {});
}
Upvotes: 3
Views: 17461
Reputation: 18647
Here is a solution in angular way:
#scrollCottom
template variable.ViewChild
to get the Element reference and should check the scroll bottom issue.Component;
import { AfterViewChecked, ElementRef, ViewChild, OnInit} from 'angular2/core'
@Component({
...
})
export class YourComponent implements OnInit, AfterViewChecked {
@ViewChild('scrollBottom') private scrollBottom: ElementRef;
ngOnInit() {
this.scrollToBottom();
}
ngAfterViewChecked() {
this.scrollToBottom();
}
scrollToBottom(): void {
try {
this.scrollBottom.nativeElement.scrollTop = this.scrollBottom.nativeElement.scrollHeight;
} catch(err) { }
}
}
HTML:
<ul #scrollBottom>
<li *ngFor="let reply of message_show.messages">
<img [src]="reply.from_user_image || '../assets/images/msg.png'"/>
<p><b>{{reply.name}} </b> <span> {{reply.updated_at | date:'dd.MM.yyyy'}} - {{reply.updated_at | date:'h:mm'}}</span></p>
<p>{{reply.text}}</p>
</li>
</ul>
Upvotes: 10
Reputation: 456
You can use
window.scrollTo(0,document.body.scrollHeight);
Here you can find the discussion related
Scroll Automatically to the Bottom of the Page
Upvotes: 5