Here is my code:
UtilisateursService.ts
public AllUsers:Utilisateur[]=[{ UserId:'',firstName:'', lastName:'',email:'',phoneNumber:null,roles:'',active:"false",createdBy:'',createdDate:null,lastModifiedBy:'',lastModifiedDate:null,},];
**public userSubject = new Subject<Utilisateur[]>();**
//userSubject2 = new Subject<Classe[]>();
emitUsers(u:Utilisateur[]) {
this.userSubject.next(u.slice());
}
getAllUsers() {
var childsdata:Utilisateur[]=[];
firebase.database().ref("/users").orderByKey().once("value").then(function(snapshot) {
snapshot.forEach(function(childSnapshot) {
childsdata.push(childSnapshot.val());
});
});
this.AllUsers=childsdata;
this.emitUsers(this.AllUsers);
console.log("this.AllUsers = ",this.AllUsers);
};
ListeUtilisateursComponent.ts
export class ListeUtilisateursComponent implements OnInit,OnDestroy {
// users: Utilisateur[];
usersSubscription: Subscription;
displayedColumns: string[] = ['nom', 'prenom', 'email', 'role','active','actions'];
**UserdataSource;**
constructor(private usersService: UtilisateursService,private router: Router) { }
ngOnInit() {
this.usersService.userSubject.subscribe((AllUsers) => {
**this.UserdataSource = AllUsers;**
});
this.usersService.getAllUsers();
// this.UserdataSource=this.usersService.getAllUsers();
console.log("Dans ngOnInit, this.UserdataSource ==== ",this.UserdataSource);
}
ListeUtilisateursComponent.html
<table mat-table [dataSource]="*UserdataSource*" class="mat-elevation-z8">
<ng-container matColumnDef="nom">
<th mat-header-cell *matHeaderCellDef> Nom </th>
<td mat-cell *matCellDef="let element"> {{element.lastName}} </td>
</ng-container>
<ng-container matColumnDef="prenom">
<th mat-header-cell *matHeaderCellDef> Prénom </th>
<td mat-cell *matCellDef="let element"> {{element.firstName}} </td>
</ng-container>
<ng-container matColumnDef="email">
<th mat-header-cell *matHeaderCellDef> Email </th>
<td mat-cell *matCellDef="let element"> {{element.email}} </td>
</ng-container>
the variable AllUsers of UtilisateurService updates correctly The variable UserdataSource is always empty, it seems that the observer doesn't work. Can you help me please ?