0
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>

App 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);          

        });
    }   
}]);

Explanation:

Ok here In firebase i have User table and it has 3 objects each with unique id. Currently I am trying to extract the unique id for object on click of Get Id button in html. But the problem is if I do console.log(id) the function getObjectId() gives me //Array [ Object, Object, Object ].. clicking on object takes me to its data and it has $id as -K9jyc0K9i28h53d23Uw..

Is there any way to get the unique id for a particular element on clicking the button

I want this unique id to be shown for element on clicking a particular element/object in html.

Please help.

2 Answers 2

1

If your user object has the id property on it, you can just refer directly to that. You can pass it as a parameter from your ng-click directive:

<button ng-click="selectUser(user)">

and in your controller:

$scope.selectUser = function(user) {
    console.log(user); // should emit the user object.  is id a property?
}

If all of your objects use the same property, you can have a single function:

$scope.showObjectId = function(object) {
    alert(object.id);
}
Sign up to request clarification or add additional context in comments.

3 Comments

User is a table name with 3 entries in array as object...object has $id now when i click on a particular object in array it should extract the $id value that is string in a variable and give back to the controller.. so i can use it in controller for furthe manipulation.. how to do tha?
thanks... but if i have the object how can i do manipulations on this like delete update etc
What about the case when i have no id?
0

You can show the id in the html like this (within the ng-repeat):

<td> {{ user.$id }} </td>

Or send it to the ng-click function (within the 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

And in you javascript you can use it like this:

$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);
}   

5 Comments

How can i use it back in my controller as a scope to further manipulate the data for deleting and editing etc.
Updated the answer. Feel free to ask questions if i'ts not clear yet.
If i take user.$id it will give me the complete array with three objects in it but if i do user[0].$id then it will give me the id for first object..actually i want the object id for particular object that i click in html but do not want to give the index of object as it should get automatically when i click GetId button and send the id back to controller
Do you have the button inside the ng-repeat in the HTML? When you use it inside the ng-repeat (so each user has his own button) it should give the id of the user you click.
by ading this ......var user = $scope.userData.$getRecord(user); console.log(user) gives //null....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.