如何在Angularfire中的数组上设置键名

Joh*_*hnK 6 angularjs firebase angularfire

我正在使用Angularfire创建数组,但我无法设置密钥名称.Firebase会自动给我一个神秘的密钥(例如Jm9vLy8Lye-KS35KsmL),但我想把它自己设置为更有意义的东西.目前还不清楚我是如何在Angularfire中做到这一点的.我在$ firebaseArray上使用$ add方法:

var firebaseRef = new Firebase("https://firebase_location"); 
$scope.messages = $firebaseArray(firebaseRef);

$scope.messages.$add( 
  {
    FirstName: patient.FirstName, 
    LastName: patient.LastName
  }
).then(function(firebaseRef) {
  var id = firebaseRef.key();
});
Run Code Online (Sandbox Code Playgroud)

数据存储得很好,我可以在仪表板上看到它.但是,id始终是一个神秘的火力值.我希望能够将自己设定为有意义的东西.在我的情况下,个别患者的ID将是有意义的...

有什么想法吗?

谢谢!

小智 0

将项目添加到 firebase 数组使 firebase 为您定义一个唯一的 id。如果这不是您想要的,我认为您可以尝试使用 $firebaseObject 获取“messages”对象,而不是仅使用 $firebaseArray 获取列表。然后您将能够在 js 中编辑您的对象,以便将您的项目添加到消息集合中。这样您就可以使用最适合您需要的 id。最后,您必须 $save() 整个对象。看这里:https ://www.firebase.com/docs/web/libraries/angular/api.html#angularfire-firebaseobject

希望这可以帮助。