1

Hi Im new in ionic im using the the ng-repeat of the angular.. and I was wondering how to get the name or id when the item of ng-repeat is clicked.. Im using sqlite to get all data.

   function UserCtrl($scope, $cordovaSQLite, $location, $state, $cordovaDialogs, $cordovaPreferences) {
        var vm = this;

        $scope.data = {};
        $scope.selection = {catagory : null};
        var query = "SELECT * FROM listpeople";
        $cordovaSQLite.execute(db, query, [])
            .then(function(res) {
                if (res.rows.length > 0) {

                    var userName = _.map(res.rows, 'name');
                    vm.names = userName;

                    console.log(userName)


                } else {
                    console.log("No results found");
                    console.log(res);
                }
            }, function(err) {
                console.error(err);
            });

} 

my html

<ion-view title="Users" ng-controller="UserCtrl as vm">
    <ion-content class="has-header padding">
        <div class="list">
            <div class="item item-icon-right" ng-repeat="users in vm.names track by $index">
                {{users}}
                <i class="icon ion-edit" ng-click = "vm.edit()"></i>
            </div>
        </div>
    </ion-content>
</ion-view>
Sachet Gupta
  • 822
  • 5
  • 18
VLR
  • 312
  • 1
  • 4
  • 18

2 Answers2

2

just pass users as a parameter of the function

 <i class="icon ion-edit" ng-click = "vm.edit(users)"></i>

controller:

vm.edit = function(users){
    console.log(users)
}
Upalr
  • 2,140
  • 2
  • 23
  • 33
Sachila Ranawaka
  • 39,756
  • 7
  • 56
  • 80
1

You can:

  1. Pass that particular item from HTML

HTML:

<div class="item item-icon-right" ng-repeat="users in vm.names track by $index">
     {{users}}
     <i class="icon ion-edit" ng-click = "vm.edit(users)"></i>
</div>

JS:

function UserCtrl($scope, $cordovaSQLite, $location, $state, $cordovaDialogs, $cordovaPreferences) {
            var vm = this;
            var vm.edit = function(item){
                //update your item here
            }
            $scope.data = {};
            $scope.selection = {catagory : null};
            var query = "SELECT * FROM listpeople";
            $cordovaSQLite.execute(db, query, [])
                .then(function(res) {
                    if (res.rows.length > 0) {

                        var userName = _.map(res.rows, 'name');
                        vm.names = userName;

                        console.log(userName)


                    } else {
                        console.log("No results found");
                        console.log(res);
                    }
                }, function(err) {
                    console.error(err);
                });

}
  1. Pass the index of that item $index from HTML

HTML:

<div class="item item-icon-right" ng-repeat="users in vm.names track by $index">
     {{users}}
     <i class="icon ion-edit" ng-click = "vm.edit($index)"></i>
</div>

JS:

function UserCtrl($scope, $cordovaSQLite, $location, $state, $cordovaDialogs, $cordovaPreferences) {
            var vm = this;
            var vm.edit = function(index){
               var itemToUpdate = vm.names[index];
               //update your item here
            }
            $scope.data = {};
            $scope.selection = {catagory : null};
            var query = "SELECT * FROM listpeople";
            $cordovaSQLite.execute(db, query, [])
                .then(function(res) {
                    if (res.rows.length > 0) {

                        var userName = _.map(res.rows, 'name');
                        vm.names = userName;

                        console.log(userName)


                    } else {
                        console.log("No results found");
                        console.log(res);
                    }
                }, function(err) {
                    console.error(err);
                });

}

In both the cases your vm.names array will have the updated value, after this edit click

Sachet Gupta
  • 822
  • 5
  • 18