0

I have a condition that needs to be checked in my view: If any user in the user list has the same name as another user, I want to display their age.

Something like

<div ng-repeat="user in userList track by $index">
 <span class="fa fa-check" ng-if="user.isSelected"></span>{{user.firstName}} <small ng-if="true">{{'AGE' | translate}} {{user.age}}</small>
</div>

except I'm missing the correct conditional

3
  • could you elaborate more on this? Commented Aug 6, 2015 at 21:32
  • if any users in user list have same first name, age(user.age) will be shown. Else age will not be shown Commented Aug 6, 2015 at 21:35
  • 2
    this kind of data manipulation is best done in the controller or through a filter. ng-repeat creates a new scope for each row in your array, and you can't compare values across scopes. Commented Aug 6, 2015 at 21:40

2 Answers 2

1

You should probably run some code in your controller that adds a flag to the user object to indicate whether or not he/she has a name that is shared by another user.

You want to minimize the amount of logic there is inside of an ng-repeat because that logic will run for every item in the ng-repeat each $digest.

I would do something like this:

controller

var currUser, tempUser;
for (var i = 0; i < $scope.userList.length; i++) {
  currUser = $scope.userList[i];
  for (var j = 0; j < $scope.userList.length; j++) {
    if (i === j) continue;
    var tempUser = $scope.userList[j];
    if (currUser.firstName === tempUser.firstName) {
      currUser.showAge = true;
    }
  }
}

html

ng-if='user.showAge'

Edit: actually, you probably won't want to do this in the controller. If you do, it'll run every time your controller loads. You only need this to happen once. To know where this should happen, I'd have to see more code, but I'd think that it should happen when a user is added.

Sign up to request clarification or add additional context in comments.

7 Comments

can i do this inside html using $index?
I'm not sure how you'd run the inner for loop. Another ng-repeat? Regardless, you really shouldn't - having an O(n^2) algorithm inside of an ng-repeat. It would be incredibly expensive. Think about it - every time there's a digest cycle, there will be n^2 loop iterations!
i am sorry. I did not understand the point. yes, i do not want to do this in controller. Wondering if there is a way i could do in html itself or use some filters. What would be the best possible way
@nikitha Not really, and running a comparison function there isn't a good idea since it has to loop through ALL your data for EVERY row. As previously stated.
I would say so. It's generally better to keep the logic out of the views.
|
0

You can simulate a hashmap key/value, and check if your map already get the property name. Moreover, you can add a show property for each objects in your $scope.userList

Controller

(function(){

function Controller($scope) {

var map = {};

$scope.userList = [{
  name:'toto',
  age: 20,
  show: false
}, {
  name:'titi',
  age: 22,
  show: false
}, {
  name: 'toto',
  age: 22,
  show: false
}];

$scope.userList.forEach(function(elm, index){
  //if the key elm.name exist in my map
  if (map.hasOwnProperty(elm.name)){
    //Push the curent index of the userList array at the key elm.name of my map
    map[elm.name].push(index);
    //For all index at the key elm.name
    map[elm.name].forEach(function(value){
      //Access to object into userList array with the index
      //And set property show to true
      $scope.userList[value].show = true;
    });
  } else {
    //create a key elm.name with an array of index as value
    map[elm.name] = [index];
  }
});


}

angular
.module('app', [])
.controller('ctrl', Controller);

})();

HTML

  <body ng-app="app" ng-controller="ctrl">

    <div ng-repeat="user in userList track by $index">
     <span class="fa fa-check"></span>{{user.name}} <small ng-if="user.show">{{'AGE'}} {{user.age}}</small>
    </div>


  </body>

3 Comments

It is quite confusing map object and using pushing index. Can you elaborate more on this?
It is a simple and efficient way to handle duplicates element. I'm pushing index to know what object i have to modify in the $scope.userList array. So if i get a key, and she is also in the map, so i've found 2 identical name, so i'll push the index where i can find this duplicate element. Do you get it ?
cool. It looks clean code and efficient. Thanks a lot for your time : )

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.