简体   繁体   English

在 angular/Typescript 上获取 map 的值

[英]Get values of map on angular/Typescript

I'm trying to get the values of a map:我正在尝试获取 map 的值:

UtilisateursService.ts UtilisateursService.ts

 getIntervenants():Map<string,string> {
          let IdDisplayNameInt = new Map();

          firebase.database().ref("/users").orderByKey().once("value")
            .then(function(snapshot) 
                    {
                      snapshot.forEach(function(childSnapshot) 
                      {

                          IdDisplayNameInt.set(childSnapshot.val().UserId,childSnapshot.val().firstName + " " +childSnapshot.val().lastName);
                      }

                      )
                     }); 
          return IdDisplayNameInt;
          }

in new-cl-component.ts i have:在 new-cl-component.ts 我有:

this.intervenantsList=this.userService.getIntervenants();
    console.log("this.intervenantsList = ",this.intervenantsList);

    console.log("this.intervenantsList.values =  ",this.intervenantsList.values());


    for (var valeur of intervenantsList.values()) {
      console.log("valeur = ",valeur);
        this.DisplayNameIntervenants.push(valeur)    }

        console.log("this.int = ",this.DisplayNameIntervenants)    ;

The map intervenantsList contains all the data that i need, map intervenantsList 包含我需要的所有数据,

this.intervenantsList but when i try to display intervenantsList.values(), it's empty:但是当我尝试显示 intervenantsList.values() 时,它是空的: map.values

i tried with a basic map, and it works我尝试使用基本的 map,它可以工作

let map = new Map();
        map.set("A",1);
        map.set("B",2);
        map.set("C",3);
        console.log("map = ",map);
        console.log("map.values= ",map.values());---- returns 1,2,3

getIntervenants() function is trying to return asynchronous data synchronously. getIntervenants() function 正在尝试同步返回异步数据。 IdDisplayNameInt variable is assigned asynchronously inside the function. IdDisplayNameInt变量在 function 内部异步分配。 You need return the value asynchronously as well.您还需要异步返回值。 One way would be to use RxJS Subject .一种方法是使用 RxJS Subject Try the following尝试以下

Service服务

import { Observable, Subject } from 'rxjs';

getIntervenants(): Observable<Map<string,string>> {
  let result = new Subject<Map<string,string>>();

  firebase.database().ref("/users").orderByKey().once("value")
    .then((snapshot) => {
      let IdDisplayNameInt = new Map();
      snapshot.forEach((childSnapshot) => {
        IdDisplayNameInt.set(childSnapshot.val().UserId,childSnapshot.val().firstName + " " +childSnapshot.val().lastName);
      });
      result.next(IdDisplayNameInt);
    });

  return result.asObservable();
}

Then you need to subscribe to the getIntervenants() function in the component然后需要订阅组件中的getIntervenants() function

this.userService.getIntervenants().subscribe(
  intervenants => { 
    this.intervenantsList = intervenants;
    console.log("this.intervenantsList = ", this.intervenantsList);
    console.log("this.intervenantsList.values =  ", this.intervenantsList.values());
    for (var valeur of intervenantsList.values()) {
      console.log("valeur = ",valeur);
      this.DisplayNameIntervenants.push(valeur);
    }
    console.log("this.int = ", this.DisplayNameIntervenants);
  }
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM