Angular / Firestore - 如何更新地图内的全局变量

时间:2017-10-12 12:03:55

标签: angular angularfire google-cloud-firestore

我正在使用

  • 公司的FireStore

我想做的事

  • 如果未返回任何数据,请更新变量

问题

  • 我似乎无法从我的snapshotChanges地图功能
  • 旁边更新任何内容

问题

  • 如何更新' noResults'变量?

组件

我指的是:if(数据类型!=='未定义'&& data.length> 0){           this.noResults = false;         }



import { Component, OnInit } from '@angular/core';

// Firestore
import { Observable } from 'rxjs/Observable';
import { AngularFirestore, AngularFirestoreCollection } from 'angularfire2/firestore';

// Routes
import { Router, ActivatedRoute } from '@angular/router';

// Moment JS
declare var moment: any;

@Component({
  selector: 'app-albums-list',
  templateUrl: './albums-list.component.html',
  styleUrls: ['./albums-list.component.css']
})

export class albumsListComponent implements OnInit {

  private albumCollection: AngularFirestoreCollection<any>;
  albums: Observable<any[]>;
  folderId: string;
  noResults = true;

  constructor(
    private readonly afs: AngularFirestore,
    private activatedRoute: ActivatedRoute,
    private router: Router
  ) { }

  
  /* ngOnInit */
  
  ngOnInit() {

    // Look at the url for the Folder ID and set the local variable
    this.activatedRoute.params.forEach((urlParameters) => {
      this.folderId = urlParameters['folderId'];
    });

    // Return the albums list
    this.getalbumListData();

  }

  
  /* RETURN album LIST DATA */
  
  getalbumListData() {

    // album Reference "folders/folderid/albums"
    this.albumCollection = this.afs.collection<any>(`folders/${this.folderId}/albums`, ref => {
      return ref.orderBy('album_title');
    });




    // Get the data
    this.albums = this.albumCollection.snapshotChanges().map(actions => {

      return actions.map(a => {

        const data = a.payload.doc.data();

        // If there's no returned data, set the 'noResults' variable
        if (typeof data !== 'undefined' && data.length > 0) {
          this.noResults = false;
        }

        const id = a.payload.doc.id;

        return { id, ...data };

      });

    });
  }





}
&#13;
&#13;
&#13;

0 个答案:

没有答案