试图让NG-CSV与火力地堡工作 [英] Trying to get ng-csv to work with Firebase

查看:94
本文介绍了试图让NG-CSV与火力地堡工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有存储在火力地堡的数据。我有一个从火力地堡抓取信息,并返回它作为一个数组的函数。我希望能够当我下载它是一个空文件,使用NG-CSV下载该文件为.csv但是。

I have data stored on Firebase. I have a function that will grab the information from Firebase and return it as an array. I want to be able to use ng-csv to download that file as a .csv however when I download it is an empty file.

时有可能使用NG-CSV,如果我想抓住从火力地堡数据,如果是没有任何人有任何的例子吗?

Is it possible to use ng-csv if I am trying to grab data from Firebase and if so does anyone have any examples?

我试图用NG-CSV允许用户通过点击一个按钮来下载.csv文件。的信息存储在火力地堡和我已经创建了返回从火力地堡为阵列的所需信息的功能。但是,我认为这个问题是,当点击该按钮之前的信息是从火力地堡和加载拉着文件下载,所以.csv文件总是空的。有没有解决的办法?这里是我的code在我main.js的应用程序:

I am trying to use ng-csv to allow a user to download a .csv file by clicking a button. The information is stored in Firebase and I have created a function that returns the needed information from Firebase as an array. However, I think the problem is when the button is clicked the file is downloaded before the information is pulled from Firebase and loaded, so the .csv file is always empty. Is there a way around this? Here is my code in my main.js app:

this.export = function() {
  var results = fireFactory.getResults() //code that returns the array of objects
  results.$loaded().then(function(array) {
    var test= [];
    test.push(array[0]);
    test.push(array[1]);
    return test;
  };
};

下面是我的code在我的HTML文件:

Here is my code in my HTML file:

<button class ="btn" ng-csv="main.export()" filename="test.csv">Export</button>

反正是有延迟的文件下载到的信息已经被加载,并从main.export()函数返回?

Is there anyway to delay the file downloading until the information has been loaded and returned from the main.export() function?

推荐答案

您几乎没有。弗兰克面包车Puffelen是在正确的道路上,但戛然而止您code提供修复程序。

You are almost there. Frank van Puffelen was on the right path, but stopped short of providing the fix for your code.

return test;

您的回调里面上面的语句返回一个承诺内的结果。这个结果只能使用承诺意识到code被消耗掉。幸运的是,NG-CSV接受的承诺。如果返回的承诺它应该工作:

the above statement inside your callback is returning the result inside a promise. This results can only be consumed using promise aware code. Fortunately, ng-csv accepts a promise. If the promise is returned it should work:

this.export = function() {
  var results = fireFactory.getResults()
  //Here we return the promise for consumption by ng-csv
  return results.$loaded().then(function(array) {
    var test= [];
    test.push(array[0]);
    test.push(array[1]);
    //the array is returned from the callback, not export
    return test;
  };
};

这篇关于试图让NG-CSV与火力地堡工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆