【问题标题】:ionic 1 with Firebase 3 get key from snapshot?带有 Firebase 3 的 ionic 1 从快照中获取密钥?
【发布时间】:2016-10-30 02:51:54
【问题描述】:
firebase.database().ref('users/' + user.uid + '/cars')

 .on('value', function(snapshot) {
    $scope.cars = snapshot.val();
    console.log('snapshot', JSON.stringify(snapshot.val()));
  });

我可以提取我需要的所有数据,但 Firebase 设置的键值除外 (-KVChuDgmbogzH2bKm1A )。它正在记录到控制台。但我无法显示它。我试过cars.key、key、$id、cars.$id

<div ng-repeat="car in cars">     

 <p>item key: {{car.key}}</p> <- this I can't get 
        <h2>{{car.color}}</h2><- this is fine
         <p>{{car.motor}}</p><-  this is fine

任何建议将不胜感激。

【问题讨论】:

    标签: javascript firebase ionic-framework firebase-realtime-database


    【解决方案1】:

    来自 Firebase 的数据正在异步加载。到数据加载完毕时,Angular 还没有准备好。

    所以你需要告诉 AngularJS 它需要更新 HTML。最简单的方法是将作用域的更新包装在 $timeout() 调用中:

    firebase.database().ref('users/' + user.uid + '/cars')    
     .on('value', function(snapshot) { $timeout() {
        $scope.cars = snapshot.val();
        console.log('snapshot', JSON.stringify(snapshot.val()));
     })});
    

    更新

    您似乎正在寻找每个快照的key

    <div ng-repeat="(key, car) in cars">     
    
     <p>item key: {{key}}</p> <- this I can't get 
        <h2>{{car.color}}</h2><- this is fine
         <p>{{car.motor}}</p><-  this is fine
    

    此时我建议改用 AngularFire,它可以为您解决很多问题。

    【讨论】:

    • 感谢您的快速回复。坦率。但是 $timeout 对我的问题没有帮助。正在返回所有 JSON 数据,因为我可以在控制台中查看所有数据。我的 html 正在按预期更新,除了 id

      item key: {{car.key}}

      {{ car.color}}{{car.motor}}
    • 我也为此添加了一个示例。请注意,虽然我最初的回复可能没有解决您的问题,但也需要$timeout()
    • 谢谢弗兰克!
      我没有调用 key !
    猜你喜欢
    • 2017-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-28
    • 2019-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多