代码之家  ›  专栏  ›  技术社区  ›  Angulandy2

如何以void数组形式提供数据

  •  -1
  • Angulandy2  · 技术社区  · 7 年前

    export class AddComponent {
      protected dataService: CompleterData;
      protected searchData = [
        {name: '84A4DA'},
        {name: '846ASD'},
        {name: '8444AS'},
    ]
    constructor(private completerService: CompleterService, private router: Router, public back: BackService,
                 ) {
    this.dataService = completerService.local(this.searchData, 'name', 'name');
    }
    

    但当我尝试从db firebase生成searchData时:

    getArray(): void {
        this.afDatabase.list('/imones')
          .valueChanges()
          .subscribe(res => {
            console.log(res)//should give you the array of percentage.
            this.array = res;
          })
    }
    
    this.searchData = this.back.getArray();
      constructor(private completerService: CompleterService, private router: Router, public back: BackService,
                 ) {
        this.dataService = completerService.local(this.searchData, 'name', 'name');
    }
    

    enter image description here

    如何修复?

    2 回复  |  直到 7 年前
        1
  •  1
  •   Eliseo    7 年前

    问题是,在构造函数中,此.SearchData不是数组。幻觉是。 2.-将代码从构造函数移到ngInit

    我很难理解您的代码,但是,如果您的服务返回的是可观察的,而不是数据,那么我支持您的组件

    constructor(private completerService: CompleterService, 
                private router: Router, public back: BackService){}
    ngOnInit()
    {
      this.back.getArray().subscribe(res=>{
          this.searchData=res;
          completerService.local(this.searchData, 'name', 'name').subscribe(res=>
          {
              this.dataService=res;
          });
      })
    }
    
    //Or , better, using switchMap
    ngOnInit()
    {
      this.back.getArray().pipe(switchMap(res=>{
          this.searchData=res;
          return completerService.local(this.searchData, 'name', 'name')
          }))
          .subscribe(res=>
          {
              this.dataService=res;
          });
    }
    
        2
  •  -1
  •   Akshay Rana    7 年前

    getArray(): void {
    this.afDatabase.list('/imones')
      .valueChanges()
      .subscribe(res => {
        console.log(res)//should give you the array of percentage.
        this.searchData = res;
      })}
    

    或者如果你一个接一个地得到值,写下

    getArray(): void {
        this.afDatabase.list('/imones')
          .valueChanges()
          .subscribe(res => {
            console.log(res)//should give you the array of percentage.
            this.searchData.push(res); //searchData must be initialized as empty array
          })
    }
    
    推荐文章