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
Post a Comment