I've encountered an issue with my code where clicking a checkbox triggers the ng-click function. Here is the JavaScript snippet:
$scope.selectTitle = function(evt, selected){
evt.stopPropagation();
var filtered = _.findWhere($scope.selectedTitles, {id: selected.id});
var index = _.indexOf($scope.selectedTitles, selected);
if(selected === filtered){
$scope.selectedTitles.splice(index, 1)
}
else{
$scope.selectedTitles.push(selected);
}
console.log('titles', $scope.selectedTitles, 'filtered', filtered, 'index', index);
};
The code was used within a table that had ng-repeat and ng-click functionalities. I utilized .stopPropagation()
to prevent the table's ng-click from being triggered.
Now, I am trying to implement a select all checkbox feature with this code:
$scope.selectAll = function (filteredTitles) {
if ($scope.selectedAll) {
$scope.selectedAll = false;
} else {
$scope.selectedAll = true;
}
_.forEach(filteredTitles, function(cpPortfolioItem) {
cpPortfolioItem.Selected = $scope.selectedAll;
if(cpPortfolioItem.Selected){
$scope.selectTitle();
}
});
However, upon running the code, an error occurs stating
TypeError: Cannot read property 'stopPropagation' of undefined
.
I cannot simply remove the stopPropagation as it serves a crucial purpose. Can anyone provide suggestions on how I can successfully select all checkboxes and execute the ng-click function for each? Your help is greatly appreciated. Thank you!