javascript - avoid adding duplicating items in ng-repeat collection, in Angular.js -


i have ng click event :

$scope.submitinfo = function myfunction() {         var itemtoadd = {             id : generateuuid(),             name: $scope.name,             email: $scope.email,             phone: $scope.phone         };         $scope.info.push(itemtoadd);     } 

my problem is, want check if same id or name not exist in $scope.info collection.

i purly foucs on angular.js, looking best solution kind of scenario

you use $filter.

something this:

$scope.submitinfo = function myfunction() {     var itemtoadd = {         id : generateuuid(),         name: $scope.name,         email: $scope.email,         phone: $scope.phone     };     var foundelement = $filter('filter')($scope.info, {name: $scope.name}, true);      if (foundelement.length === 0) {         $scope.info.push(itemtoadd);     } }; 

of course, you'll need inject $filter in controller/factory/whatever


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -