10.8: Sending Messages
Now we'll work on getting the send message functionality from the PhotoEditor component functional.
Add sendMessage() to UserService
We'll start off by adding a sendMessage() method to the UserService. This will again be very similar to other methods we've written:
sendMessage(id: number, message: Message) {
return this._http.post<Message>(this.baseUrl + 'users/' + id + '/messages', message);
}Update MemberMessagesComponent
Add a new property to member-messages.component.ts called newMessage.
newMessage: any = {};Also, add a new method called sendMessage():
sendMessage() {
this.newMessage.recipientId = this.userId;
this._userService.sendMessage(this._authService.decodedToken.nameid, this.newMessage).subscribe(message => {
this.messages.unshift(message);
this.newMessage.content = '';
}, error => {
this._alertify.error(error);
});
}Update MemberMessges Template
Next, in member-messages.component.html - update the <form> in the footer to:
It should work now! Open the browser and test it out.

Last updated