I'm trying to create a delete service with angular.
Here is my controller :
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 problem is here
});
};
});
and the service called :
$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 get the error :
undefined is not a function
at DeleteAMessage
I understand that my function doesn't return any promise but I don't know how I could make this work, I just want my redirection with $location.path done only if the user has clicked on yes in my modal windows.
I wanted to add a "then" to wait the answer of the user before doing the redirection.
It looks like I should create a promise but can't figure how I can "create" a promise. When I use $http.get I understand what's in the promise but here I can't (before no data is expected, I just want to know when the user has clicked on yes).
Thank you
You're trying to call .then()
on a bool, which of course isn't going to work. The AngularJS documentation includes very easy-to-understand examples on using $q (its flavor of promises).
From the docs:
// for the purpose of this example let's assume that variables `$q` and `okToGreet`
// are available in the current lexical scope (they could have been injected or passed in).
function asyncGreet(name) {
var deferred = $q.defer();
setTimeout(function() {
deferred.notify('About to greet ' + name + '.');
if (okToGreet(name)) {
deferred.resolve('Hello, ' + name + '!');
} else {
deferred.reject('Greeting ' + name + ' is not allowed.');
}
}, 1000);
return deferred.promise;
}
var promise = asyncGreet('Robin Hood');
promise.then(function(greeting) {
alert('Success: ' + greeting);
}, function(reason) {
alert('Failed: ' + reason);
}, function(update) {
alert('Got notification: ' + update);
});
Here is how you would introduce promise (with $q service) in your script :
$this.SupprimerMessage = function(message){
//var retour = true;//no more need
var defer = $q.defer(); //inject $q into your service via dependency injection - here create a promise
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;
defer.resolve({message:"Message corectly deleted"});
},function(){//this is the error callback if you used $http for SupprimerMessageFromServer
defer.reject({error:"Something went wrong while deleting message"});
});
});
});
}
else{
defer.reject({error:"No message defined"});//this will go to the error callback of the promise
}
return defer.promise;//whatever return a promise on which you'll be able to call .then()
};
Just add a parameter (function type) for callback
$this.SupprimerMessage = function(message, callback){
....
/* user pressed ok */
listeMessages[index]._id = 0;
callback();
....
}
$this.DeleteAMessage = function(){
dataRefreshServices.SupprimerMessage($scope.messageToWatch, function() {
$location.path('/messages');
});
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.