如何通过 angularjs 从 firebase 数据库中获取数组元素的唯一 ID
how to get the unique id of array element on click through angularjs from firebase database
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>
应用 Js:
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 中,我有用户 table,它有 3 个对象,每个对象都有唯一的 ID。目前,我正在尝试通过单击 html 中的“获取 ID”按钮来提取对象的唯一 ID。但问题是如果我做 console.log(id) 函数 getObjectId() 给我 //Array [ Object, Object, Object ].. 点击对象带我到它的数据并且它有 $id as -K9jyc0K9i28h53d23Uw ..
有什么方法可以在单击按钮时获取特定元素的唯一 ID
我希望在单击 html 中的特定 element/object 时为元素显示此唯一 ID。
请帮忙。
如果您的用户对象上有 ID 属性,您可以直接引用它。您可以将其作为参数从您的 ng-click 指令中传递:
<button ng-click="selectUser(user)">
并在您的控制器中:
$scope.selectUser = function(user) {
console.log(user); // should emit the user object. is id a property?
}
如果您的所有对象都使用相同的 属性,您可以拥有一个函数:
$scope.showObjectId = function(object) {
alert(object.id);
}
您可以像这样在 html 中显示 id(在 ng-repeat 中):
<td> {{ user.$id }} </td>
或者将其发送到 ng-click 函数(在 ng-repeat 中):
<button class="btn btn-warning" ng-click="getObjectId(user.$id)">Get Id</button> //Just send the id to the function
<button class="btn btn-warning" ng-click="getObjectId(user)">Get Id</button> //Send the etire user object to the function
在你 javascript 你可以这样使用它:
$scope.getObjectId = function(user){
//If you only send the id to this function you can get the entire user object like this (it gives a $firebaseObject i think):
var user = $scope.userData.$getRecord(user);
//Here you can do anything with the user
//Update user data
$scope.UserData.$save(user);
//Remove user
$scope.UserData.$remove(user);
}
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>
应用 Js:
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 中,我有用户 table,它有 3 个对象,每个对象都有唯一的 ID。目前,我正在尝试通过单击 html 中的“获取 ID”按钮来提取对象的唯一 ID。但问题是如果我做 console.log(id) 函数 getObjectId() 给我 //Array [ Object, Object, Object ].. 点击对象带我到它的数据并且它有 $id as -K9jyc0K9i28h53d23Uw ..
有什么方法可以在单击按钮时获取特定元素的唯一 ID
我希望在单击 html 中的特定 element/object 时为元素显示此唯一 ID。
请帮忙。
如果您的用户对象上有 ID 属性,您可以直接引用它。您可以将其作为参数从您的 ng-click 指令中传递:
<button ng-click="selectUser(user)">
并在您的控制器中:
$scope.selectUser = function(user) {
console.log(user); // should emit the user object. is id a property?
}
如果您的所有对象都使用相同的 属性,您可以拥有一个函数:
$scope.showObjectId = function(object) {
alert(object.id);
}
您可以像这样在 html 中显示 id(在 ng-repeat 中):
<td> {{ user.$id }} </td>
或者将其发送到 ng-click 函数(在 ng-repeat 中):
<button class="btn btn-warning" ng-click="getObjectId(user.$id)">Get Id</button> //Just send the id to the function
<button class="btn btn-warning" ng-click="getObjectId(user)">Get Id</button> //Send the etire user object to the function
在你 javascript 你可以这样使用它:
$scope.getObjectId = function(user){
//If you only send the id to this function you can get the entire user object like this (it gives a $firebaseObject i think):
var user = $scope.userData.$getRecord(user);
//Here you can do anything with the user
//Update user data
$scope.UserData.$save(user);
//Remove user
$scope.UserData.$remove(user);
}