【问题标题】:Using jquery, how to close pop-up modal (ui-bootstrap) dynamically without click event in Angularjs使用jquery,如何在Angularjs中动态关闭弹出模式(ui-bootstrap)而不点击事件
【发布时间】:2016-03-01 08:08:30
【问题描述】:

:模态

<script type="text/ng-template" id="create.html">
<div ng-controller="CreateCampaignCtrl" id="createModal">
    <div class="modal-header">
       heading
    </div>
    <div class="modal-body">
       ----
    </div>
    <div class="modal-footer">
        <button type="button" class="btn btn-white" ng-click="cancel()">Close</button>
        <button type="button" class="btn btn-primary" ng-click="submit_form()"><i class="fa fa-plus"></i>create method</button>
    </div>
</div>

:控制器

function CreateCampaignCtrl($scope, $http){       
$scope.submit_form = function(){
        $http({
            url: url,
            data: {},
            method: "POST",
            headers: {'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'}
        }).success(function(data){
                $('#createModal').modal('hide');
                alert(data);
        }).error(function(err) { alert('ERROR err');})
    }
};

};

上面的模态是在单击按钮时发出 ajax 请求时打开的,现在我想在得到服务器的响应后动态关闭这个模态。

【问题讨论】:

  • 你为什么不在角度控制器代码中尝试$modalInstanse.close()
  • 您好 Pankaj,我尝试使用此方法,但无法正常工作。感谢您的回复。
  • 你在使用angular-ui-bootstrap/jQuery bootstrap modal吗?
  • 我正在使用 angular-ui-bootstrap
  • 那么$modalInstanse.close() 应该可以工作.. 可能你在控制器中错过了$modalInstanse.. 你能创建一个有问题的plunkr 吗?

标签: angularjs


【解决方案1】:

最后我得到了在 angularjs 中关闭 UI-Bootstrap POP-UP Modal 的解决方案:

function getRetiredData($scope, $http, $modalStack){
    $http({
        url : base_url,
        data : {},
        method : "POST",
        headers : {'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'}
    }).success(function(data){
        $modalStack.dismissAll();
    }).error(function(err){
        alert(err);
    });
};

};

在你的控制器中注入 $modalStack 并像这样调用函数 $modalStack.dismissAll();它关闭所有打开的模式。

【讨论】:

    【解决方案2】:

    编辑:

    好吧,放弃我的第一个想法。真的很简单

    modalInstance.close()
    

    我刚刚创建了一个 2 秒的超时来模拟你的承诺。

    $timeout(function(){modalInstance.close();},2000);
    

    plunkerhere

    【讨论】:

    • 您好 Lee,感谢您的回复,我尝试使用 $('#id).modal('close') 它没有关闭模态,而是引发错误。
    • @VenkateshS 不认为你可以提供错误?
    • TypeError: data[option] 不是函数
    • 感谢您的回复,我终于找到了解决方案。
    猜你喜欢
    • 1970-01-01
    • 2013-05-18
    • 1970-01-01
    • 2018-12-17
    • 2016-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-09
    相关资源
    最近更新 更多