2016-02-05 18 views
0

我正在嘗試使用Angular-Strap和$alert服務在我的AngularJS應用程序中實現自動警報。然而,迄今爲止這麼好,我留下了一個我似乎無法解決的問題。Angular-Strap Alerts:在alert.hide()觸發時無法捕獲

當我使用duration屬性或alert.hide()命令隱藏$alert時,我可以設置一個回調來捕獲回調嗎?我想在警報進入隱藏狀態時運行一個函數。

我的代碼片段看起來是這樣的:

var alertContent = ''; 


     // Define the critical alert 
     var criticalAlert = $alert({ 
          templateUrl: 'templates/critical.alert.tpl.html', 
          title: ' Critical Alert Detected!', 
          content: alertContent, 
          container: 'body', 
          type: 'danger', 
          dismissable: false, 
          duration: '20', 
          show: false 
         }); 

...

alertContent = 'Foo Bar!'; 

...

criticalAlert.$promise.then(criticalAlert.hide); 

...

$scope.$on('alert.hide', function() { 
      console.log('Alert Hidden'); 
     }); 
+0

我已經找到了合適的解決方案,您的問題? –

回答

0

$promise的結果,你可以通過你自己的功能,如:

criticalAlert.$promise.then(function(){criticalAlert.hide();console.log('Alert Hidden'); }) 

更多$promiseanglular $q

UPDATE

您可以使用$watch

當然,這不是一個很好的解決方案,但我找到了最好的解決方案。至少,它的工作原理!

var alert = $alert({ 
    title: ' Critical Alert Detected!', 
    content: 'its Alert!', 
    container: 'body', 
    type: 'danger', 
    dismissable: false, 
    duration: '5', 
    show: false, 
}); 
$scope.$watch(function(){return alert.$isShown;},function(val){ 
    if(val===true) 
    console.log('alert opened',val); 
    else 
    console.log('alert closed',val); 
}); 
alert.$promise.then(alert.show); 

更新2

我們可以使用的角度event

jsfiddle上的示例。

.controller('ExampleController', function($scope, $alert) { 
$scope.alert = $alert({ 
    title: ' Critical Alert Detected!', 
    content: 'its Alert!', 
    container: '#divForAlert', 
    type: 'danger', 
    dismissable: false, 
    duration: '5', 
    show: false, 
    prefixEvent:"mymodal", 
}); 
$scope.alert.$promise.then(function() { 
    $scope.alert.show(); 
}); 
$scope.$root.$on('mymodal.hide.before', function(event) { 
    console.log('hide before',event); 
}); 
$scope.$root.$on('mymodal.hide', function(alert) { 
    console.log('hide',alert); 
});}); 

有些重要 HTML

<div ng-controller="ExampleController"> 
<h3> 
    ExampleController 
</h3> 
<form name="ExampleForm" id="ExampleForm"> 
    <div id="divForAlert"> 

    </div> 
</form> 

+0

什麼時候由於持續時間屬性自動關閉? – Riples

+0

另外,有沒有一種方法可以捕獲.hide.before()? – Riples

+0

我更新了我的答案。查看。 –