I am new to Angular, to explain myself properly I am going to start from an example.
When we have the Firebase library installed in our project, we can call the information from the Users table as follows:
constructor(db:AngularFirestore){
db.collection('Users').valueChanges().subscribe((data: User[]) => {
this.users= data;
console.log(this.users);
});
}
The valueChanges() method is responsible for listening to any changes in the database and updating them in the user view without the need to refresh it (this is when making a manual change on a certain field in the same Firebase database).
So, how can this result be achieved, if for example I am consuming an API Rest? If when making a direct change in the database, this change is reflected in the user's view without the need to refresh.
// users.service.ts
get():Observable<any>{
return this.httpClient.get(this.API_ENDPOINT+'/users_location');
}
// user.component.ts
users: Users[]=[];
constructor(private userService: UsersService) {
this.getTrailers();
}
getUsers(){
this.userService.get().subscribe( (data:Users[])=>{
this.users= data;
});
}