2014-10-10 56 views
0

我只是试图设置一个警告对话框,并试用Bootstrap模式为初学者。Bootstrap模式对话框和角度绑定不起作用

引导模式:http://getbootstrap.com/javascript/#modals

模态的火灾,但它并没有显示我的modalTitle和modalBody值!

HTML片段这里(注意vm.modalTitle和vm.modalBody范围瓦尔) -

<!-- Bootstrap Modal Dialog Template--> 
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> 
<div class="modal-dialog"> 
    <div class="modal-content"> 
     <div class="modal-header"> 
      <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button> 
      <h4 class="modal-title" id="myModalLabel"><span ng-model="vm.modalTitle"></span></h4> 
     </div> 
     <div class="modal-body"><span ng-model="vm.modalBody"></span></div> 
     <div class="modal-footer"> 
      <button type="button" class="btn btn-primary" data-dismiss="modal">OK</button> 
     </div> 
    </div> 
</div> 

和我的控制器代码:

vm.modalTitle = ''; vars initialized at the top of my controller 
vm.modalBody = ''; 
... 
function sendRequest(){ 
     if (data.status == 'FAIL') { 
       if (data.messages.length > 0) { 
        logErr("Error submitting aggregation request: " + data.messages[0]); 
        vm.modalTitle = "Error submitting aggregation query !"; 
        vm.modalBody = data.messages[0]; 
        $('#myModal').modal('toggle'); 
        $('#myModal').on('hide.bs.modal', function (e) { 
         // fired when modal is hidden from user 
        }) 
        return; 
       } 
    } 
    } 

模态的火灾,但它确实不显示我的modalTitle和modalBody值!

+1

你是怎么做的要求?我猜测Angular没有意识到vm模型中的变化,并且视图没有被更新。尝试在切换模式之前使用$ apply()启动摘要循环。 – Bema 2014-10-10 16:45:21

+0

@Bema,如果我发出$ scope。$ apply();那么错误说“$摘要已在进行中”。 – 2014-10-10 18:39:22

回答

2

当我第一次学习Angular时,我尝试了这条路线,很快就对我必须做的黑客工作感到沮丧。你最好的选择是使用AngularUI模式。它很好地包装Bootstrap模态。 http://angular-ui.github.io/bootstrap/

+0

是的,我实际上已经下载了角度ui bootstrap,但我也发现它是一种代码笨重和尴尬。也许我应该重新考虑一下。 – 2014-10-10 19:00:51

+0

AngularStrap(http://mgcrea.github.io/angular-strap/)也是一个可行的选项。无论哪种方法都比尝试自己开发一些东西更好,特别是在开始时。 – MBielski 2014-10-10 20:48:39