首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Firebase:记录子查询的$scoping数组

Firebase:记录子查询的$scoping数组
EN

Stack Overflow用户
提问于 2016-10-31 10:04:20
回答 1查看 50关注 0票数 1

我有一个AngularJS应用程序创建待办事项列表,并将它们保存到Firebase ($FirebaseArray)上。我想要显示一个图表,显示属性显示为completed: true的item对象和属性显示为completed: false的对象之间的对比。为了做到这一点,我必须为每种项目创建两个不同的数组。

我可以设法console.log这些数组的结果,但是通过$scope在DOM中显示这些数组很困难,更不用说将它们提供给Angular-ChartJS脚本了。

下面是我的Firebase的样子:

代码语言:javascript
复制
{
  "items": {
    "KUnjnUG90g4Ms_pkbC5": {
      text: "I need to do my hw",
      dueDate: 1477647527093,
      hoursToFinish: 1,
      minutesToFinish: 5,
      importance: "job depends on it",
      completed: true,
      pastDue: true,
      urgent: true,
      rank: 4089044218,
      created_at: 1477263177719
    },
   "KUq51IiPh4RgaC_v0Rg": {
      text: "asdf",
      dueDate: 1477647527093,
      hoursToFinish: 1
      minutesToFinish: 5
      importance: "pretty important"
      completed: false,
      pastDue: true
      urgent: true
      rank: 3792736666
      created_at: 1477302560019
    }
  }
}  

工厂处理有问题的控制器的ref

代码语言:javascript
复制
listo.factory("ItemCrud", ["$firebaseArray",
  function($firebaseArray) {

    var ref = new Firebase("database");
    var items = $firebaseArray(ref);
    ...

    return {
      getAllItems: function() {
        return items;
      }
    }
    ...
  }
]);

下面是有问题的查询($scope.updateArrays)的控制器:

代码语言:javascript
复制
listo.controller('UserCtrl', ["$scope", "ItemCrud", "$rootScope", "$interval", "$log", "$http", "$locale", "$templateCache", '$timeout',
   function($scope, ItemCrud, $rootScope, $interval, $log, $http, $locale, $templateCache, $timeout) {

  $scope.items = ItemCrud.getAllItems();

  $scope.completeItems = [];
  $scope.incompleteItems = [];

  $scope.updateArrays = function() {
    var items = ItemCrud.getAllItems();
    items.$loaded().then(function(items) {

      items.$ref().orderByChild("q_completed").equalTo(true).on("value", function(snapshot) {
        console.log("updateArrays called");

        var completeItems = [];
        completeItems = Object.keys(snapshot.val());

        if (snapshot.exists()) {
          console.log("found", completeItems);
          return completeItems;
        } else {
          console.warn("completeItems was not found.");
          return [];
        }

      });

      items.$ref().orderByChild("q_completed").equalTo(false).on("value", function(snapshot) {

        var incompleteItems = [];
        incompleteItems = Object.keys(snapshot.val());

        if (snapshot.exists()) {
          console.log("found", incompleteItems);
          return incompleteItems;
        } else {
          console.warn("incompleteItems was not found.");
          return [];
        }

      });

      return {
        incompleteItems: incompleteItems,
        totalIncompleteItems: incompleteItems.length,
        completeItems: completeItems,
        totalCompleteItems: completeItems.length
      }

    });
  };

 }
]);

简单地问一下我的问题:为了在DOM上显示$scope.updateArrays().totalIncompleteItems$scope.updateArrays().totalCompleteItems,我必须进行哪些编码?

也就是说,如何显示以下内容:

代码语言:javascript
复制
<section ng-controller="UserCtrl" class="user">
    {{$scope.updateArrays().totalIncompleteItems}} and {{$scope.updateArrays().totalCompleteItems}}
</section>

到目前为止,在我的应用程序中,这些没有显示出来。但是,$scope.updateArrays()内部的console.log将以下内容登录到我的控制台:

代码语言:javascript
复制
updateArrays called
UserCtrl.js:495 found completeItems array ["-KUnjnUG90g4Ms_pkbC5", "-KUq51IiPh4RgaC_v0Rg", "-KUq6pXzElRP9JQC6AJB", "-KUq7WvMzH4FNaLdGL90"]
UserCtrl.js:510 found incompleteItems array ["-KVMv-hKRzk-WXv-KrOv", "-KVMv1nIC26elEBX7QA-", "-KVMv3qAtVNTW0j7sU8K", "-KVMv5smhYTeaDFGYmoh"]] 

如果有人对Firebase查询有深入的了解,我将非常感激。

EN

回答 1

Stack Overflow用户

发布于 2016-10-31 14:37:28

你得到的是键而不是值。

修改以下代码

代码语言:javascript
复制
var completeItems = [];
completeItems = Object.keys(snapshot.val());

if (snapshot.exists()) {
  console.log("found", completeItems);
  return completeItems;
} else {
  console.warn("completeItems was not found.");
  return [];
}

到这里面去。

代码语言:javascript
复制
snapshot.forEach(function(childSnapshot) {
  completeItems.push(childSnapshot.val());
});

console.log("found", completeItems);

对未完成的项目执行相同的操作

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40335674

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档