4

I'm Trying to notify the user each time a duplicate is registered in a table. I would like to catch the: Error: [ngRepeat:dupes] and alert the user before proceeding. Here is the controller method adding elements to the table:

$scope.addProdukt = function (item) { kladdValidationService.checkTable(); try { $scope.addedResult.push(item); // works fine logService.info('Produktet er lagt til i tabellen'); } catch (e) { console.log(e.stack); console.log($exceptionHandler(e)); // neither of these two work } console.log($scope.addedResult); }; 

Every time I push an already added item to the table, the Error: [ngRepeat:dupes] outputs in the console. Juswt not sure how to catch it.

4 Answers 4

3

The reason the error isn't caught is because pushing the duplicate into the array is perfectly valid and doesn't cause an exception. the exception happens in the ng-repeat directive.

If you wish to notify your user when they try to add a duplicate try something like this:

$scope.addProdukt = function (item) { if(addedResult.indexOf(item) != -1){ console.log("You tried to add a duplicate"); }else{ $scope.addedResult.push(item); } } 

If you wanted to warn the user about a duplicate but still let them have duplicates then you can show it in the html by adding track by $index in your ng-repeat i.e:

<div ng-repeat="item in $scope.addedResult track by $index"> </div> 
Sign up to request clarification or add additional context in comments.

Comments

0

You can try followings:

track by $index always remove the duplicasy

<div ng-repeat="n in [42, 42, 43, 43] track by $index"> {{n}} </div> 

1 Comment

The duplicate by itself is not the issue. My problem is that a duplicate in the table produces a unvalid state that the user can't see unless the console is open. I need to load an alert for example, to make the user coorect their mistake
0

Something along this lines? Unfortunately, I can not test this right now but the core was taken from anguarJS sample code that describes how to log exceptions

angular. module('exceptionOverwrite', []). factory('$exceptionHandler', [function() { var exceptions = []; return function myExceptionHandler(exception, cause) { if(exceptions.indexOf(exception) !== -1) {// its duplicate do your thing } this.exceptions.add(exception) }; }]); 

Comments

0

I would like to catch the: Error: [ngRepeat:dupes] and alert the user before proceeding.

To show to the users that your code crashed with Exception?

Its not good practice to catch Exceptions of your code and notify users about. Keep in mind that after Error: [ngRepeat:dupes] - your code doesn't work properly! It can cause to unexpected behavior. Digest cycle stops and so on.

I strongly suggest you to write logic that searches in arrays duplicate ids and notify users.

Don't use Angular Exception flow for your needs!


Anyways if you want to manage exceptions, you can catch override $exceptionHandler :

For example:

app.config(function($provide) { $provide.decorator("$exceptionHandler", function($delegate, $injector) { return function(exception, cause) { var data = { message: "Exception: " + exception.message, stack: exception.stack || "none" }; // manage your exception alert(JSON.stringify(data)); // Call The original Angular Exception Handler $delegate(exception, cause); }; }); }); 

Demo Plunker

After you can inject some service and pass all Exception data there:

// ... var someService = $injector.get("SomeService"); someService.reportClientError(data).then(function(data) { //... }, function(error) { //... }); 

1 Comment

The array containg the items added and the table rendering them are seperate. ng-repeat will only show the item once even though its added twice in the array which will be used later in the application.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.