2014-12-22 16 views
1

我正在嘗試創建一個帶角度的刪除服務。如何使一個然後功能的工作?

這裏是我的控制器:

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的重定向只有在用戶在我的模態窗口中點擊了yes。

我想在做重定向之前添加一個「then」來等待用戶的回答。

看起來我應該創造一個承諾,但不能確定如何「創造」承諾。當我使用$ http.get時,我瞭解承諾的內容,但在這裏我不能(在沒有預期數據之前,我只想知道用戶何時點擊了是)。

謝謝

回答

1

只需添加一個參數(功能型)回調

$this.SupprimerMessage = function(message, callback){ 
.... 
/* user pressed ok */ 
listeMessages[index]._id = 0; 
callback(); 
.... 
} 

$this.DeleteAMessage = function(){ 
    dataRefreshServices.SupprimerMessage($scope.messageToWatch, function() { 
     $location.path('/messages');   
    }); 
}; 
+0

不錯的解決方案謝謝你! – user2088807

2

你試圖調用.then()在布爾,這當然是行不通的。 AngularJS documentation包含使用$ 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); 
}); 
1

這裏是你將如何在腳本中引入的承諾(與$ 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() 
    }; 
+0

謝謝,我記住這一點 – user2088807