HTML:
<div ng-controller="getIdController">
<div class="container">
<div class="row">
<div class="col-md-6">
<hr />
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>Gender</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="user in userData">
<td> {{ user.name }} </td>
<td> {{ user.age }} </td>
<td> {{ user.gender }} </td>
<td><button class="btn btn-warning" ng-click="getObjectId()">Get Id</button></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
应用程序J:
var crudApp = angular.module('crudApp', ['firebase']);
crudApp.controller('getIdController', ['$scope', '$firebaseArray', function($scope, $firebaseArray){
var ref = new Firebase('https://blistering-fire-4719.firebaseio.com/users');
$scope.userData = $firebaseArray(ref);
$scope.getObjectId = function(){
$scope.userData.$loaded()
.then(function(id){
var obj = id;
console.log(obj);
});
}
}]);
说明:
好吧,在 firebase 中,我有 User 表,它有 3 个对象,每个对象都有唯一的 id。目前,我正在尝试通过单击 html 中的“获取 Id”按钮来提取对象的唯一 ID。但问题是,如果我执行 console.log(id) ,函数 getObjectId() 会给我//Array [ Object, Object, Object ].. 单击对象会将我带到它的数据,并且它的 $id 为 -K9jyc0K9i28h53d23Uw ..
有什么方法可以在单击按钮时获取特定元素的唯一 ID
我希望在单击 html 中的特定元素/对象时显示元素的唯一 ID。
请帮忙。
最佳答案
如果您的用户对象具有 id 属性,您可以直接引用它。您可以将其作为 ng-click 指令中的参数传递:
<button ng-click="selectUser(user)">
在你的 Controller 中:
$scope.selectUser = function(user) {
console.log(user); // should emit the user object. is id a property?
}
如果所有对象都使用相同的属性,您可以拥有一个函数:
$scope.showObjectId = function(object) {
alert(object.id);
}
关于javascript - 如何从Firebase数据库中通过AngularJS点击时获取数组元素的唯一ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35224329/