I took back a project using Angularjs. There is a filtering system.
Actually, a click on a category add this category to a list. All industries that got the category are shown. Note that a click on an already selected category remove this category.
I would like a click to replace the filtering category so that each click do not add the category. How should I modify this code:
$scope.changerCategorie = function(category , name ){
if( jQuery.inArray(category, $scope.selected ) !== -1)
{
angular.forEach($scope.selected , function(obj,index){
if( category == obj ){
$scope.selected.splice(index,1);
$scope.selected_name.splice(index,1);
}
});
}else{
$scope.selected.push(category);
$scope.selected_name.push(name);
}
$scope.selected_filter = $scope.selected.join(', ');
}
I tried this but it doesn’t work
$scope.changerCategorie = function(category , name ){
$scope.selected = 0;
$scope.selected_name = 0;
$scope.selected = category;
$scope.selected_name = name;
$scope.selected_filter = $scope.selected.join(', ');
}
and I get
$scope.selected.join is not a function
If I remove the line related to selected.join I got another error after two clicks:
Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys. Repeater: tags in selected_name, Duplicate key: string:l
How should I do?
I believe the preferred way to empty an array in javascript is to set its length to 0:
$scope.selected.length = 0
.The reason you are getting an error on join() is that you are now setting
$scope.selected
directly to the category, which is not an array. This would fix that:$scope.selected = [category];
Of course, you only need one of these methods. Either an empty followed by a push, or the direct assignment. Also if you are planning on completely getting rid of the multiple filter feature, it may be better in the long run to store it as just a category rather than an array.