使用AngularFire v2获取关键值有点清晰?

 香烟迷住双眼 发布于 2023-02-10 12:30

对不起这个长度,但它让我有点疯狂:

假设我想在加载时获得项目的"标题"和".priority"键值.

首先让我们看看每件事的布局:

 $scope.items = $firebase(new Firebase("https://****.firebaseio.com"));

 $scope.items.$on('loaded', function() { 

console.log($scope.items);

});

有了这个,我们得到:

> Object {$bind: function, $add: function, $save: function, $set: function, $remove: function…}
    > $add: function (item, cb) {
    > $bind: function (scope, name) {
    > $child: function (key) {
    > $getIndex: function () {
    > $on: function (type, callback) {
    > $remove: function (key) {
    > $save: function (key) {
    > $set: function (newValue) {

    > this is my item: Object
          $$hashKey: "012"
          $id: "this is my item"
          $priority: 2884707
          title: "this is the title of my item"
          __proto__: Object


    > __proto__: Object

看起来不错,让我们尝试抓住优先级:

 console.log($scope.items.$child('$priority'));

哎呀:

Uncaught Error: Firebase.child failed: First argument was an invalid path: "$priority". Paths must be non-empty strings and can't contain ".", "#", "$", "[", or "]"

好的,我们暂时跳过这个,只是尝试标题:

console.log($scope.items.$child('title'));

> Object {$bind: function, $add: function, $save: function, $set: function, $remove: function…}
    > $add: function (item, cb) {
    > $bind: function (scope, name) {
    > $child: function (key) {
    > $getIndex: function () {
    > $on: function (type, callback) {
    > $remove: function (key) {
    > $save: function (key) {
    > $set: function (newValue) {
    > __proto__: Object

无题.

好的,让我们以不同的方式尝试:

    var firstItem = $scope.items.$getIndex()[0];  //returns $ids in order of priority
     console.log($scope.items.$child(firstItem));

     > Object {$bind: function, $add: function, $save: function, $set: function, $remove: function…}
         > $add: function (item, cb) {
         > $bind: function (scope, name) {
         > $child: function (key) {
         > $getIndex: function () {
         > $on: function (type, callback) {
         > $remove: function (key) {
         > $save: function (key) {
         > $set: function (newValue) {
              title: "this is the title of my item"
         > __proto__: Object

但现在没有优先权!

厨房水槽在计算机时间发誓:

 console.log($scope.items.title);

     > undefined

 console.log($scope.items[0].title);

     > Uncaught TypeError: Cannot read property 'title' of undefined 

  console.log(Object.keys($scope.items));     

     > ["$bind", "$add", "$save", "$set", "$remove", "$child", "$on", "$getIndex"]

我错过了什么?我可以通过做几次掉头来获得'头衔',(弄清楚该项目的$ id是使用'$ getIndex()',抓住第n个项目,然后使用'$ child()'再次打电话),但即便如此也不适合优先考虑.是否有更简单的方法从AngularFire v2中获取关键值?或者以任何方式获得$优先权?

1 个回答
  • 我相信我遇到了同样的问题.希望这与您的经历有关.即:

    $scope.items = $firebase(new Firebase("https://xxxxxx.firebaseio.com"));
    $scope.items.$on('loaded', function() {
      console.log($scope.items['some key']); // this writes undefined to console
    });
    

    我注意到的是,如果我不依赖这个事件,这确实有效loaded.例如在HTML中:

    <a href="#" data-ng-click="logItem()">click me</a>
    

    在JS中:

    $scope.items = $firebase(new Firebase("https://xxxxxx.firebaseio.com"));
    $scope.logItem = function() {
      console.log($scope.items['some key']); // this works
    }
    

    单击"单击我"链接会按预期将我的项目记录到控制台.

    感觉像loaded时间的集合没有完全填充firebase的基础项目.

    2023-02-10 12:34 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有