2016-08-04 68 views
1

我有一个列表/一个数组组织的对象数组,我认为这个对象名称是一个'id'。我正在使用firebase,我无法弄清楚如何获取身份证号码本身。任何帮助将是巨大的在firebase中检索对象的名称

//js 
    myFirebaseRef.once("value", function(snapshot) { 
    var list = snapshot.val(); 
    $scope.items = list 
    console.log($scope.items) 
    }) 
//log $scope.items 
[1: Object, 2: Object, 3: Object, 4: Object, 5: Object, 6: Object, 7: Object //... 

回答

1

angular.forEach一样,你可以做一些附近:

angular.forEach($scope.items,function(key,value){ 
    //the key will contain your id 
    console.log(key); 
}) 
+0

感谢,但它记录对象及其项目,而不是对象的ID /名称'对象{德:“咖啡厅”,名称:“乔的咖啡馆”,速度:“2”}' –

+0

我有在我的模板中获取该ID。它应该是什么样子? {{key}}?不工作 –

+0

你的意思是对象retreive有一个属性名称?如果是这种情况,您可以使用函数 –

1

这是你在找什么?

myFirebaseRef.once("value", function(snapshot) { 
    //var list = snapshot.val(); 
    snapshot.forEach(function(itemSnapshot) { 
    console.log(itemSnapshot.key); // if you're using 2.x that is key() 
    }); 
}) 
0

像这样。您可以使用obj[key] = value;来设置变量的ID。

function update(node,key,value){ 
    var ref = firebase.database().ref('/'); 
    var obj = {}; 
    obj[key] = value; 
    ref.child(node).update(obj) 
    .then(function() { 
     console.log('Update Ran Successfully'); 
    });  
}