2015-05-29 59 views
4

我可以告訴一個可以忽略的警告只隱藏,而不是在被解散時從dom中刪除它本身嗎?隱藏引導程序中的禁止警報

<div class="alert alert-success alert-dismissible fade in" role="alert" if.bind="message"> 
    <button type="button" class="close" data-dismiss="alert" aria-label="Close"> 
     <span aria-hidden="true">&times;</span> 
    </button> 
    <strong>Success!</strong> ${message} 
</div> 

問題是我現在沒有使用jQuery來處理任何事情。我需要aurelia根據標記或存在message來顯示/隱藏它。

它可以工作一次,但只要我解僱它,它就會從DOM中刪除,這樣新的消息就不會出現。

+0

相反的if.bind你可以使用一個撥動類像'$ {駁回:「隱藏」:「」}' –

回答

3

我可能會做這樣的......

<div class="alert alert-success alert-dismissible fade-in ${dismissed ? 'hidden': ''}" role="alert"> 
    <button type="button" class="close" click.delegate="dismiss" data-dismiss="alert" aria-label="Close"> 
    <span aria-hidden="true">&times;</span> 
    </button> 
    <strong>Success!</strong> ${message} 
</div> 

而且在js

dismiss() { 
    this.dismissed = this.dismissed? true: false; 
} 

如果有一個更清潔的方式做到這一點我很想聽聽吧:)

4

除了if.bind,還有一個show.bindmanual),或者您可以使用hidden.bind綁定到默認的隱藏屬性。

就像Matt說,你也可以用串插添加CSS類手動${!message ? 'hidden': ''}

+1

我認爲show.bind是什麼@demo在這種情況下尋找,因爲它只是將元素設置爲'display:none;'但將它保存在DOM中。 –

+0

謝謝,我知道有一個更好的方法:)我是我的情況我不使用隱藏,但另一個類,所以我在那裏添加一些轉換。 –

0

對於我的閃光的容器,顯示來自AJAX調用我建立了一個偵聽到close.bs.alert事件響應消息並添加刪除HTML再次到DOM。

$("#flash-container").on("close.bs.alert","#flash-alert", 
/* The container that will hold the #flash-alert. 
* This setup is neccessary so that the event will be caught 
* when the HTML is added once again. 
*/ 
    function (e) {  
     var $alert = $(this).addClass("d-none"); 
     /* Remove the existing text */ 
     $alert.find("#flash-container-text").empty(); 
     /* e.delegateTarget will be #flash-container */ 
     e.delegateTarget.innerHTML = $alert[0].outerHTML;  
});