2014-12-17 3 views
1

앵귤러 부트 스트랩에서 경고를 추가하려고하는데 타임 아웃시 자체를 닫으려고합니다. 그러나 그것은 그 자체를 일축하지 않습니다. 다음은 코드입니다.앵귤러 부트 스트랩 경고가 타임 아웃시 해제되지 않습니다.

angular.module('ui.services').service('AlertService', [ 
    function() { 
    var alerts = []; 
    this.add = function(type, msg){ 
     var self = this;    
     return alerts.push({ 
      type: type, 
      msg: msg, 
      close: function() { 
       return self.closeAlert(this); 
      } 
     }); 
     $timeout(function(){ 
      self.closeAlert(this); 
     }, 3000); 

    }, 
    this.closeAlert = function(alert) { 
     return this.closeAlertIdx(alerts.indexOf(alert)); 
    }, 
    this.closeAlertIdx = function(index) { 
     return alerts.splice(index, 1); 
    }, 
    this.alertData = function() { 
     return alerts; 
    } 
}]); 

무엇이 잘못 되었습니까?

+0

(); 컨트롤러 B에서 AlertService.add ('success', 'this is deleted', true);로 표시 할 경고를 추가합니다. 그래서 경고 서비스 – looneytunes

답변

2

방금 ​​전에 반환 했으므로 $timeout이 호출되지 않습니다.

그렇지 않으면 한눈에 모든 것이 올바른 것으로 보입니다.

+0

에 정의 된 로컬 경고 배열에 $ scope .alerts를 묶어두고 있으므로 여기에 반환하기 전에 teh 시간 초과를 어떻게 실행할 수 있는지 잘 모르겠다. – looneytunes

+1

무슨 뜻입니까? 그것을 배치하여 _before_ 반환 –

+0

예, 알 겠어,하지만이 경우에는 두 컨트롤러 사이에 사용되는 $ scope 속성이 나중에 경고 배열로 초기화되므로 경고가 나타나지 않으므로 경고가 표시되지 않습니다. 경고 서비스 호출 – looneytunes

관련 문제