Angular 按观察者角度/类型脚本更新变量
这是我的密码: 利用服务Angular 按观察者角度/类型脚本更新变量,angular,typescript,observable,subject,Angular,Typescript,Observable,Subject,这是我的密码: 利用服务 public AllUsers:Utilisateur[]=[{ UserId:'',firstName:'', lastName:'',email:'',phoneNumber:null,roles:'',active:"false",createdBy:'',createdDate:null,lastModifiedBy:'',lastModifiedDate:null,},]; **public userSubject = new Subject<Ut
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);
};
ListUsiateUrsComponent.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>
笔名
{{element.lastName}
名词
{{element.firstName}
电子邮件
{{element.email}
UtilizateUrse服务的可变用户将正确更新
变量UserdataSource总是空的,似乎观测者不工作。
你能帮我吗?把它们放在你的请求里。这是一个异步调用,当您在请求之外尝试时,您的数据可能仍然无法填充
getAllUsers() {
var that=this;
var childsdata:Utilisateur[]=[];
firebase.database().ref("/users").orderByKey().once("value").then(function(snapshot) {
snapshot.forEach(function(childSnapshot) {
childsdata.push(childSnapshot.val());
that.AllUsers=childsdata;
that.emitUsers(that.AllUsers);
console.log("that.AllUsers = ",that.AllUsers);
});
});
};
服务和组件中存在问题。在这两种情况下,异步数据都是同步访问的 服务
childsdata
是异步分配的,因此this.alluser=childsdata代码>语句和依赖于此的其他语句。诱惑者
应在然后
附件中
要访问回调中的成员变量,应该用箭头函数替换用function
关键字定义的函数。这个
关键字在传统JS函数中的含义表示函数的范围,在箭头函数中表示类
详情请参阅:
public alluser:usilisateur[]=[{UserId:'',firstName:'',lastName:'',email:'',phoneNumber:null,roles:'',active:“false”,createdBy:'',createdDate:null,lastModifiedBy:'',lastModifiedDate:null},];
public userSubject=新主题();
排放用户(u:Usilizateur[]){
this.userSubject.next(u.slice());
}
getAllUsers(){
let Childs数据:利用率[]=[];
firebase.database().ref(“/users”).orderByKey().once(“value”).then((快照)=>{/{/{
this.UserdataSource=alluser;
log(“Dans ngOnInit,this.UserdataSource==”,this.UserdataSource);
});
this.usersService.getAllUsers();
}
}
有关如何访问异步数据的更多详细信息,请参见此处:错误错误:未捕获(承诺中):TypeError:无法设置未定义TypeError的属性'AllUsers'。错误:无法设置未定义TypeError的属性'AllUsers'。完整错误请参见我的评论:)它表示未定义,因为'this'与函数(childSnapshot)相关在foreach so中,我们将服务的“this”更改为“that”
getAllUsers() {
var that=this;
var childsdata:Utilisateur[]=[];
firebase.database().ref("/users").orderByKey().once("value").then(function(snapshot) {
snapshot.forEach(function(childSnapshot) {
childsdata.push(childSnapshot.val());
that.AllUsers=childsdata;
that.emitUsers(that.AllUsers);
console.log("that.AllUsers = ",that.AllUsers);
});
});
};