2016-09-15 50 views
1

我試圖給超時清除我的FlashService消息。但它起到延遲時間的作用。超時清除Angularjs中的Flash消息?

FlashService.Success(("Removed Successfully"), false); 

在此我使用false作爲條件。這意味着當頁面或位置改變時,閃光消息被清除。

flash.service.js

function clearFlashMessage() { 
       var flash = $rootScope.flash; 
       if (flash) { 
        if (!flash.keepAfterLocationChange) { 
         delete $rootScope.flash; 
        } else { 
         // only keep for a single location change 
         flash.keepAfterLocationChange = false; 
        } 
       } 
      } 
     } 

     function Success(message, keepAfterLocationChange) { 
      $rootScope.flash = { 
       message: message, 
       type: 'success', 
       keepAfterLocationChange: keepAfterLocationChange 
      }; 
     } 

     function Error(message, keepAfterLocationChange) { 
      $rootScope.flash = { 
       message: message, 
       type: 'error', 
       keepAfterLocationChange: keepAfterLocationChange 
      }; 
     } 

在我上面的js我與標誌清除閃光燈消息「假」時,頁面或位置改變。

我需要在該假條件下設置超時。這就是說,如果標誌是假的,那麼閃存消息需要在一段時間內被清除。

+0

這只是延緩顯示消息,你就需要在該服務的變化我想 – taguenizy

+0

它顯示2000毫秒後,因爲你給了2000毫秒的延遲。這不是你想要的嗎? –

+0

它是如何$超時的作品。它在給定的延遲後解析功能。我想你必須在2000毫秒後退出FlashService ..然後你必須做一些工作 –

回答

1

您需要調用函數以在2秒後清除消息 - 而不是在做$timeout(fn, interval)。 即

FlashService.Success(("Removed Successfully"), false); 
$timeout(function(){ 
    //clear message 
    //FlashService.ClearMessage(); - or whatever how you clear the message 
}, 2000); 
+0

它只清除成功消息。但div是可見的。如上所示在我編輯的代碼中。 – kalai

+0

@kalai:您需要向您展示'div' html以獲得相應的答案......另外,顯示'FlashService'如何顯示消息將有所幫助。 – Paritosh

+0

@kalai:[如何創建一個最小,完整和可驗證的示例](http://stackoverflow.com/help/mcve) – Paritosh