Listen to modal click

时间:2018-08-22 13:44:29

标签: angularjs

This is angularjs. For some reason, I need to create the popup inside the controller. So I have something like this:

var popupTemplate =
            '<div class="modal fade">' +
            '  <div class="modal-dialog">' +
            '    <div class="modal-content">' +
            '      <div class="modal-header">' +
            '        <button type="button" class="close" data-dismiss="modal">&times;</button>' +
            '      </div>' +
            '      <div class="modal-body">' +
            '      <div>You sure?</div>' +
            '      </div>' +
            '      <div class="modal-footer">' +
            '        <button type="button" class="btn btn-primary" data-dismiss="modal">OK</button>' +
            '        <button type="button" class="btn btn-link" data-dismiss="modal">Cancel</button>' +
            '      </div>' +
            '    </div>' +
            '  </div>' +
            '</div>';

To open the modal I run this:

var themodal = $(popupTemplate).modal();                

I need to know if the user clicked on the OK-button. Unfortunately adding an "ng-click" causes problems. Nothing is executed. I have read a little about compiling and/or using data-ng-click. But none of those worked.

So I was thinking about listening to the promise. But having:

themodal.then(function(){
            console.log("test");
        })  

Results in the error "themodal.then is not a function".

I then tried running this instead:

var dialog = $modal.open(popupTemplate);
dialog.result.then(function(){
    //Do stuff with respect to closure
});           

But I then get a "$modal-open is not a fucntion".

Even though I included '$modal' in my app.

Which of them should I use and how can I make one of these approaches work?

1 个答案:

答案 0 :(得分:0)

请查看我在here中的回答,我相信它将为您提供帮助,并为您提供模态用法的另一种视角。