如何使 then 函数工作
How to make a then function work?
我正在尝试创建一个带有角度的删除服务。
这是我的控制器:
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
});
};
});
和称为:
$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;
};
我收到错误:
undefined is not a function
at DeleteAMessage
我知道我的函数没有返回任何承诺,但我不知道我该如何做到这一点,我只想在用户在我的模态窗口中单击是时才使用 $location.path 进行重定向。
我想添加一个"then"来等待用户的回答,然后再进行重定向。
看起来我应该创建一个承诺,但不知道如何"创建"一个承诺。当我使用 $http.get 时,我了解承诺中的内容,但在这里我不能(在没有预期数据之前,我只想知道用户何时单击是)。
谢谢
你试图在布尔值上调用.then()
,这当然是行不通的。AngularJS文档包括使用$q(其承诺的风格)的非常易于理解的示例。
从文档中:
// 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);
});
只需添加一个参数(函数类型)进行回调
$this.SupprimerMessage = function(message, callback){
....
/* user pressed ok */
listeMessages[index]._id = 0;
callback();
....
}
$this.DeleteAMessage = function(){
dataRefreshServices.SupprimerMessage($scope.messageToWatch, function() {
$location.path('/messages');
});
};
以下是您在脚本中引入 promise ($q 服务)的方法:
$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()
};
相关文章:
- 咖啡的Javascript订单总数无法使函数正常工作.请协助
- removeClass函数没有'工作不正常
- 你能解释一下这个阶乘函数是如何工作的吗?
- JavaScript链接在点击时不起作用;函数在页面加载时工作
- jQuery加载的async和ready函数不工作
- 更改智能表中的页面将停止JQuery函数的工作
- 为什么不是'我的函数在解析云代码中工作吗?当我在Angular和Express中测试时,它是有效的
- jQuery函数不能只在一个页面上工作
- 如何检查这个代码点火器php函数是否工作,该函数是否在ajax url中使用
- 从js引擎的角度来看闭包和构造函数是如何工作的
- appendChild在函数外部工作,但在函数内部不工作
- 如何在php中创建一个函数,该函数与文本区域一起工作,通过输入类似[color:red]的内容来打印具有等效颜色的文本
- 由于某种原因,我的JavaScript函数无法工作
- 为什么递归生成器函数没有't在ES2015工作
- 我需要一个jQuery函数来只工作在700px以上的屏幕大小,无法在我的代码中发现错误
- 从控制台中的工作函数转换为工作脚本.(请参阅编辑)
- Angular JS-工作函数中的“错误:[$interpole:interr]Can't interpole:
- 简化工作函数以替换特定单词
- 为什么不在回调中工作函数?
- InnerHTML不工作:函数未定义