简体   繁体   English

如何从angularjs关闭引导模式窗口

[英]How to close bootstrap modal window from angularjs

I am using modal window for the upload of images. 我正在使用模式窗口上传图像。 But when click on upload I want to upload the images and then close the modal window. 但是,当单击“上传”时,我要上传图像,然后关闭模式窗口。 When I used data-dismiss="modal" as attribute in button it just closed the window and did nothing else. 当我在按钮中使用data-dismiss="modal"作为属性时,它只是关闭了窗口,没有执行任何其他操作。 So I found some options how to close it but everything was with the help of Jquery eg: $('#my-modal').modal('hide'); 所以我找到了一些如何关闭它的选项,但是一切都在Jquery的帮助下完成,例如: $('#my-modal').modal('hide'); but I am using angular and try to do it in controller but I did not succeeded. 但是我正在使用angular并尝试在控制器中做到这一点,但是我没有成功。 I also try these answers Closing Bootstrap modal onclick but with no luck. 我也尝试这些答案关闭Bootstrap模态onclick,但是没有运气。

Finally I did it this way: 最终,我以这种方式做到了:

document.getElementById('fileUpload').value = null;
var dialog = document.getElementById('complete-dialog');
dialog.style.display = 'none';
dialog.className = 'modal fade';
dialog.setAttribute('aria-hidden', 'true');
dialog.removeChild(dialog.firstChild);

and it closes modal window but then I cannot scroll on my web page. 并且它关闭了模式窗口,但随后我无法在网页上滚动。 I have no idea how to do it better. 我不知道如何做得更好。

Can anybody help me to figure it out how to close the modal window from angularjs or at least how can I use jquery inside of angularjs? 谁能帮我弄清楚如何从angularjs关闭模态窗口,或者至少如何在angularjs内使用jquery?

EDIT: HTML where I open modal window: 编辑:HTML,我打开模式窗口:

<div class="image col-lg-2 col-lg-offset-2 col-md-3 col-md-offset-1 col-sm-3 col-sm-offset-1">
<button class="btn btn-fab btn-raised btn-primary" data-toggle="modal" data-target="#complete-dialog"><i class="mdi-image-edit"></i></button>
</div>

HTML modal window: HTML模式窗口:

<div id="complete-dialog" class="modal fade" tabindex="-1">
        <div id="test-dialog" class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close close-btn" data-dismiss="modal" aria-hidden="true">×</button>
                    <h4 class="modal-title" style="color: black;">Upload profile picture</h4>
                </div>

                <div class="modal-body" style="color: black;">
                    <form name="userForm" data-ng-submit="profile.uploadImageToServer()" autocomplete="off">
                        <div class="form-group">
                            <!--<label for="fileUpload">Profile image</label>-->
                            <input type="file" name="fileUpload" id="fileUpload" onchange="angular.element(this).scope().uploadImg(this.files)">
                        </div>
                        <div class="text-center form-group">
                            <button id="submit-btn" type="submit" class="btn btn-large btn-primary">Upload Image</button>
                        </div>
                    </form>

                </div>
                <!--<div class="modal-footer">-->
                <!--</div>-->
                </form>
            </div>
        </div>
    </div>

Angular controller: 角度控制器:

me.uploadImageToServer = function(){
            console.log('uploading');
            console.log($scope.file);
            var fd = new FormData();
            //Take the first selected file
            fd.append('file', $scope.file[0]);

            if($scope.file.length>0) {

                me.user.image = '/img/'+ me.user._id + (($scope.file[0].type.indexOf('png') > -1) ? '.png' : '.jpg');
                User.update(me.user, function(response){
                    AuthenticationState.user = response.data;
                }, function(response){
                    console.log(response);
                });

                User.uploadImage(fd, function (response) {
                    console.log(response);
                    me.user.image = me.user.image + '?time=' + new Date().getTime();
                    AuthenticationState.user.image = me.user.image;
                    document.getElementById('fileUpload').value = null;
                    //var dialog = document.getElementById('complete-dialog');
                    //dialog.style.display = 'none';
                    //dialog.className = 'modal fade';
                    //dialog.setAttribute('aria-hidden', 'true');
                    //dialog.removeChild(dialog.firstChild);
                    $scope.$close(me.file);
                    me.file = [];
                }, function (response) {
                    console.log(response);
                    me.file = [];
                });
            }
        };

You haven't really shown how your app is structured, but from the ui-bootstrap documentation: 您尚未真正显示出应用程序的结构,但是从ui-bootstrap文档中可以看到:

The open method returns a modal instance, an object with the following properties: open方法返回一个模式实例,一个具有以下属性的对象:

close(result) - a method that can be used to close a modal, passing a result close(result) -一种可用于关闭模式并传递结果的方法

In addition the scope associated with modal's content is augmented with 2 methods: 此外,与模态内容相关的范围还通过2种方法来扩大:

$close(result) $关闭(结果)

Probably the latter is the one you want to use, so you would have something like this when you click the upload button: 后者可能是您要使用的那个,因此,当您单击上upload按钮时,您将得到如下所示的内容:

<button ng-click="uploadAndClose()"> Upload </button>

Controller: 控制器:

$scope.uploadAndClose = function() {
  // your upload code, e.g.:
  $upload($scope.file);
  // This line closes the modal.
  $scope.$close($scope.file);
}

After your edits: 编辑后:

It looks to me like you are trying to use bootstrap in its 'vanilla' form rather than using the excellent ui-bootstrap module. 在我看来,您正在尝试以“原始”形式使用引导程序,而不是使用出色的ui-bootstrap模块。 This will be very awkward and is outside the scope of a single stackoverflow question. 这将非常尴尬,并且不在单个stackoverflow问题的范围之内。

More importantly, though, it shows you don't have a good understanding of angular - angular is excellent for creating re-useable components, and leveraging bootstrap or jquery plugins is very rarely a good idea. 但是,更重要的是,它表明您对angular没有足够的了解-angular 非常适合创建可重用的组件,而利用bootstrap或jquery插件很少是一个好主意。

I suggest improving your knowledge of angular, particularly the fundamental concepts of directives and services. 我建议您增加有关角度的知识,尤其是指令和服务的基本概念。 I highly recommend following the thinkster.io course on the subject. 我强烈建议您遵循有关该主题的thinkster.io课程。

最后,我通过调用关闭按钮上的click事件解决了我的问题,该按钮做了data-dismiss =“ modal”并关闭了modal对话框。

<div class="{{model_show?'model fade content':'modal-content'}}" role="model"><br><button ng-click="model_show = 1" data-dismiss="modal">Close</button>

只需设置课程条件,然后单击关闭按钮即可更改model_show = 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM