我正在使用與您相同的工具(敲除和引導程序3)。我這是怎麼做到情態動詞爲例:
這個div存在的身體在我的index.html的第一個元素:
<div class="modal fade" id="BaseModal" tabindex="-1" role="dialog" aria-labelledby="BaseModal" aria-hidden="true" data-backdrop="static" data-keyboard="false">
<div id="modalLoading" class="ajax-loader">
<img src="images/working_blue.gif" />
</div>
<div class="modal-dialog modal-lg">
<div id="ModalAnchor" class="modal-content col-lg-4 col-lg-offset-4" data-bind="html: $data.ModalContent"></div>
</div>
</div>
// This is typescript but the javascript should be pretty close to this.
private LoadModalView(viewName: string, viewModel: any): void {
$.get(viewName)
.then((view: any, status: string, jxhr: JQueryXHR) => {
this.ModalContent(''); // ensure there is no content in the modal.
this.ModalContent(view); // Load new HTML
ko.applyBindingsToDescendants(viewModel, this.ModalAnchor[0]); // bind new modal viewmodel
this.BaseModal.modal('show'); // Open the modal
}, (view: any, status: string, jxhr: JQueryXHR) => { /*Do error handling*/ });
}
編輯:這是我裝入的HTML的例子模態:
<div class="modal-header">
<h3 data-bind="text: $data.Header"></h3>
</div>
<div class="modal-body">
<h5 data-bind="text: $data.Message"></h5>
</div>
<div class="modal-footer">
<button id="OK" class="btn btn-primary center-block" data-bind="click: OK, text: ButtonText" />
</div>
在LoadModalView功能,視圖模型參數是代表新的模態的新數據/內容/等一個視圖模型。
在模態Viewmodel中,我在模態中調用'hide'時會提供目的。
我希望這有助於!我不知道這是否是「正確」或「正確」的方式來做到這一點,但這是我的模式。
我實際上已經想出了這一點,在更多的方式...但這也可以...並應被視爲答案。鑑於這必須是一個非常普遍的現象,我認爲淘汰賽應該記錄這個或者他們推薦的任何方法。 – Ronnyek 2014-09-11 16:48:05