Need ability to remove messages without the controller or view changing
Here's my case scenario: I'm building an upload feature. If the user selects a file with bad data, we show an error message and give them a chance to re-upload the file once they've addressed the changes. Once they start to re-upload the file, I need to be able to remove the error messages and start fresh.
The only way I can do that with this directive is to force the $rootScope.mcMessages message to change.
// function that gets triggered when user initiates upload
$scope.upload = function() {
// First, remove any errors that might have occurred from a previous upload attempt
messageCenterService.reset();
// Shouldn't have to explicitly do this next part but unfortunately I have to
$rootScope.mcMessages = messageCenterService.mcMessages;
// Parse the file
if ( errors occurred ) {
messageCenterService.add("danger", "Error occurred, please try again!");
// Shouldn't have to explicitly do this next part but unfortunately I have to
$rootScope.mcMessages = messageCenterService.mcMessages;
}
}
I feel like the directive itself should be what's refreshing the $rootScope.mcMessages array. Why do I have to do this explicitly myself? Am I missing something here?
I feel like the directive itself should be what's refreshing the $rootScope.mcMessages array. Why do I have to do this explicitly myself? Am I missing something here?
It's been quite some time since I last fiddled with the intricacies of the code, so I'm not sure how to answer those questions. Did you try implementing it? Did tests pass? Can you share the code as a PR?
Thanks for contributing!
Hi Mateu,
My code above exists inside my controller, not your import.js. We need to find a way to get $rootScope inside of the service, and add $rootScope.mcMessages = messageCenterService.mcMessages; at the bottom of the add function. That would probably fix it, I think. The problem is that I can't figure out how to inject $rootScope into a service. It may not even be possible.
The odd part is that $rootScope.mcMessages should be updating automatically each time its assigned array is updated, but it's not. If it did, our whole problem would go away.
Really, the whole goal here is to add and remove messages from the screen without the view / controller changing and without a timeout, but instead based on a user-triggered action. If there's a way to do that with the current code base, I'm all ears.
Hi @martynchamberlin,
Some time has passed since your last comment, but just to let you know that I was struggling with the same thing but was able to accomplish that using a combination of markShown and removeShown.
It would be nice if reset could accomplish the exact same thing but, at least, we're able to do it without messing with the internal mcMessages.
Anyway, just wanted to let you know.
Cheers.