I have a simple Angular 6 app with follow and unfollow buttons. When you click follow, the number increases. I want to save these follower numbers to a JSON server.
Here is the link to the JSON server documentation: JSON Server Documentation
To see a demo of what I'm trying to do, click here: demo. Here is my JSON file:
"data": [
{
"id": 1,
"following": 121,
"followers": 723,
}
],
Below is the HTML code:
<div class="container">
<div class="row">
<p class="col">{{numberOffollowers}}</p>
<button class="col btn btn-success" (click)="followButtonClick()">Follow</button>
<button class="col btn btn-danger" (click)="unfollowButtonClick()">Unfollow</button>
</div>
</div>
Here is the service:
import { Injectable } from '@angular/core';
import { Statuses} from '../model/statuses';
import { HttpClient } from '@angular/common/http';
@Injectable()
export class UsersService {
status: Statuses[];
constructor(private http: HttpClient) { }
statusUrl = 'http://localhost:3000/statuses';
getStatuses() {
return this.http.get<Statuses[]>(this.statusUrl);
}
addStatus(status: Statuses) {
return this.http.patch(this.statusUrl, status);
}
}
Here is the TypeScript file:
import { Component, Input } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { UsersService } from './service/users.service';
import { Statuses} from './model/statuses';
@Component({
selector: 'like-box',
templateUrl: 'like.component.html',
styleUrls: [ './like.component.css' ]
})
export class LikeComponent {
numberOffollowers : number = 69;
status: Statuses[];
constructor(private http: HttpClient, private usersService: UsersService) { }
followButtonClick() {
this.numberOffollowers++;
}
unfollowButtonClick() {
this.numberOffollowers--;
}
addStatus() {
this.usersService.addStatus(this.numberOffollowers)
.subscribe(data => {
this.status.push(this.numberOffollowers);
});
}
}
Check out my app by visiting this link: app.
The current setup is not working. What adjustments are needed in my app to successfully save those followers' numbers to the JSON server?