I have three components: userlist page, userdetails page, and inside userdetails there is a subcomponent (Child) that displays a list of users like (Related), when I enter user1 I see his name and email and on the other side there is a that will list the rest of the usernames, when I select another user to see the user details, the URL change without any problem but the user Details does not change Until I reload page after that the new user data appears on user Details.
UserListComponent:
users!: any;
constructor(private userService: UserService) { }
ngOnInit(): void {
// To show All users
this.userService.getAllUsers().subscribe((data) => {
this.users = data;
}, (error) => {
console.warn("Error");
})
}
// UserListComponent.html
<div *ngFor="let user of users">
<h3><a [routerLink]="['users', user.id]"> {{user.name}}</a></h3>
</div>
UserDetailsComponent.ts:
public id!: any;
public user!: any;
constructor(private actRouted: ActivatedRoute, private userServise: UserService) {
this.id = this.actRouted.snapshot.paramMap.get('id');
}
ngOnInit(): void {
this.actRouted.params.subscribe(
(params: Params) => {
this.id = params['id'];
}
)
// To show Selected user
this.userServise.getUserById(this.id).subscribe((data) => {
this.user = data;
},(error) => {
console.warn("error");
})
}
//UserDetailsComponent.html
<h3>{{user.email}}</h3>
<h3>{{user.name}}</h3>
<div class="center">
// this child will pass name of selected User
<app-child-details [passTCh]="this.user"></app-child-details>
</div>
ChildDetailsComponent.ts:
@Input() passTCh: any;
data: any;
filtered_array: any;
constructor(private userServise: UserService) { }
ngOnInit(): void {
// console.log(this.passTCh.id);
const Observable$ = new Observable((observer) => {
this.userServise.getAllUsers().subscribe((data) => {
this.data = data;
// I Filter userSelcetd !== id
this.filtered_array = this.data.filter((value: any) => value.id != this.passTCh.id);
console.log("detail***********", this.filtered_array);
observer.next(observer);
}, (error) => {
console.warn("Error", error);
})
});
Observable$.subscribe(d => {
console.log(d)
})
}
//ChildDetailsComponent.html
<h6>{{passTCh.name}}</h6>
<hr />
<div *ngFor="let user of this.filtered_array">
<h6><a [routerLink]="['/users', user.id]"> {{user.name}}</a></h6>
</div>
UserService
getAllUsers() {
return this.http.get(this.baseUrl);
}
getUserById(id:any) {
return this.http.get(this.baseUrl + id);
}
I want these components to change dynamically without reload page