13
  $scope.displayyears = [];
  $scope.Joinyear = function(display) {
    $scope.yeardisplay = display;       
    $scope.yeardisp = $scope.displayyears.push($scope.yeardisplay);
    $scope.displayyearss = uniq($scope.yeardisp)
  }

it throws error like "uniq is undefined"..How we check uniqueness??

1

1 Answer 1

24

Try checking if the yeardisplay is already in the array before you add it

$scope.displayyears = [];
$scope.Joinyear=function(display){
     $scope.yeardisplay=display;        
     if ($scope.displayyears.indexOf(display) == -1) {
         $scope.displayyears.push(display);
     }
}
Sign up to request clarification or add additional context in comments.

5 Comments

You are welcome :) You can mark the question as 'Answered' by clicking the checkbox (below the voty of the answer). This will save time for other readers who look for unanswered questions, and serve as a 'thank you' to the person who wrote the answer (no need to comment 'thank-you's in this website).
is the line '$scope.yeardisplay=display; ' necessary? just curious?
Definitely not necessary, I don't know what's it for. I'm guessing it is displayed somewhere on his page. I was just modifying the push+uniq combo into valid js code and left the rest of it untouched.
FYI, this does not work in IE8 and below because they don't support "indexOf"
@b2238488 you are right, thanks. For older browsers the same can be accomplished with jQuery's inArray: api.jquery.com/jQuery.inArray but Angular.js doesn't support IE8 as well so I think its safe to use indexOf for an Angular.js question.

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.