I'm currently working on implementing a delete service in Angular.
Below is my controller code :
app.controller('VoirMessagesController', function($scope, $rootScope, $routeParams, $location,$translate, userService, dataRefreshServices){
$scope.messageToWatch = dataRefreshServices.getMessageToWatch();
this.DeleteAMessage = function(){
dataRefreshServices.SupprimerMessage($scope.messageToWatch).then(function(){
$location.path('/messages'); // The issue arises here
});
};
});
and the corresponding service code:
$this.SupprimerMessage = function(message){
var retour = true;
if(message != undefined)
{
$translate(['MESSAGES_MESSAGERIE_SUPPRIMER', 'BUTTON_CANCEL', 'MESSAGES_MESSAGERIE_MESSAGE_VALIDATION_SUPPRESSION_TEXTE', 'MESSAGES_MESSAGERIE_MESSAGE_VALIDATION_SUPPRESSION_TITRE']).then(function(translations)
{
var modalOptions = {
closeButtonText: translations.BUTTON_CANCEL,
actionButtonText: translations.MESSAGES_MESSAGERIE_SUPPRIMER,
headerText: translations.MESSAGES_MESSAGERIE_MESSAGE_VALIDATION_SUPPRESSION_TITRE,
bodyText: translations.MESSAGES_MESSAGERIE_MESSAGE_VALIDATION_SUPPRESSION_TEXTE
};
// displaying the modal box
modalYesNoService.showModal({}, modalOptions).then(function (result) {
var index = _.indexOf(listeMessages, _.find(listeMessages, function (_message) { return _message._id == message._id; }));
$this.SupprimerMessageFromServer(message).then(function(promise){
listeMessages[index]._id = 0;
});
});
});
}
return retour;
};
I am encountering the following error:
undefined is not a function
at DeleteAMessage
I realize that my function does not return a promise, but I am unsure how to rectify this. My goal is to redirect using $location.path only after the user has clicked "yes" in the modal window.
I thought about adding a "then" clause to wait for user input before initiating the redirection.
It seems like I need to create a promise, but I am uncertain of the process. Unlike with $http.get where data is expected, in this case, I just need confirmation when the user clicks "yes".
Thank you