AngularJS: User object

982 Views Asked by At

I want to have an object that will save data for the login user in my web. I want him to count how many times the user have been connected, how many times the user paged between the pages, how many times the user ordered the table and how many times the user clicked on his name.

I build this object :

$scope.User = {
    'username': '',
    'Password': '',
    'connected': false,
    'countConnect': 0,
    'countPaging':0,
    'countOrder':0,
    'countTapName':0
};

and this are example of functions for the counts :

    $scope.tapName = function(){
        if ($scope.username == $scope.User.username){
            $scope.User.countTapName ++;
        }
    };

    $scope.countOrder = function (){
       $scope.User.countOrder++;
    };

    $scope.pageChanged = function() {
        console.log('Page changed to: ' + $scope.currentPage);
        $scope.User.countPaging ++;
    };

my problem is that all the users get the same counts , and not for individual user.

the html :

<td ng-click="tapName()" ng-model="un" class="col-lg-1">{{user.userN}}</td>
        <td class="col-lg-1">{{user.PassW}}</td>
        <td class="col-lg-1">{{user.Name}}</td>
        <td class="col-lg-1">{{user.LastName}}</td>
        <td class="col-lg-1">{{User.countConnect}}</td>
        <td class="col-lg-1">{{User.countPaging}}</td>
        <td class="col-lg-1">{{User.countOrder}}</td>
        <td class="col-lg-1">{{User.countTapName}}</td>

is there a way to do it better ?

1

There are 1 best solutions below

1
Sebastien Horin On BEST ANSWER
  • use un array of users:

$scope.Users = [user1,user2,...]

  • use the ng-repeat directive:

https://docs.angularjs.org/api/ng/directive/ngRepeat

<div ng-repeat="user in Users">
        <div ng-click="tapName(Users.user)">
           <div>{{$index}}</div>
           <div>{{user.Name}}</div>
           ...
        </div>
</div>
  • and pass your user:

    $scope.tapName = function(user){ user.countTapName ++; };