gpt4 book ai didi

javascript - 从 javascript 打开 Bootstrap 模式对话框不起作用

转载 作者:行者123 更新时间:2023-11-30 16:54:32 25 4
gpt4 key购买 nike

谁能告诉我为什么模式对话框在单击按钮时没有打开?我收到错误消息:“document.getElementById(...).modal 不是函数”。请帮助:)

这是我的 html:

<body ng-app='ModalDemo'>
<div ng-controller='MyCtrl'>
<button ng-click ="open()">Open</button>
<div class="modal custom-modal fade" id="myModal" tabindex="-1" role="dialog" aria-hidden="true">
<div class="vertical-alignment-helper">
<div class="modal-dialog custom-modal-dialog vertical-align-center">
<div class="modal-content custom-modal-content">
<div class="modal-body custom-modal-body">
<div class="custom-modal-inside">
<p>Calculating Rates, Price & Fees ...</p>
<p>
<img src="ajax-loader.gif">
</p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>

这里是 javasript 函数 open():

app = angular.module('ModalDemo', []);
app.controller('MyCtrl', ['$scope', function($scope) {
$scope.open = function() {
document.getElementById('myModal').modal({ show: true, backdrop: false, keyboard: false });
};
}]);

如果用这个替换按钮标签,一切正常:

<button type="button" class="btn btn-default" data-toggle="modal" data-target="#myModal" data-backdrop="static" data-keyboard="false" >

但我需要从 javasript 函数打开模态,为什么它不起作用?

最佳答案

试试这个 Plunker:http://plnkr.co/edit/PPaGgFJbe8QcAU80lNm5?p=preview

这使您的模态使用与 Angular 方式保持一致。

希望这对你有用!

HTML:

  <head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<link data-require="bootstrap-css@*" data-semver="3.3.1" rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" />
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<script data-require="angular.js@1.3.x" src="https://code.angularjs.org/1.3.15/angular.js" data-semver="1.3.15"></script>
<script data-require="ui-bootstrap@0.12.1" data-semver="0.12.1" src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.12.1.min.js"></script>
<script src="app.js"></script>
</head>

<body ng-controller="MainCtrl">
<div>
Click to open: <a class="btn btn-primary" ng-click="Open()">Open Modal</a>
</div>
</body>

</html>

Javascript:

var app = angular.module('plunker', ['ui.bootstrap']);

app.controller('MainCtrl', function($scope,$modal) {
$scope.name = 'World';

$scope.Open = function(){
var modalInstance = $modal.open({
templateUrl: 'modal.html',
controller: 'confirmmodalController',
controllerAs: 'confirmmodalCtrl',
size: 'sm'
});

modalInstance.result.then(function () {
// Ok
}, function () {
// Cancel
});
}
})
.controller('confirmmodalController', function ($modalInstance) {
var self = this;

self.ok = function () {
$modalInstance.close();
};

self.cancel = function () {
$modalInstance.dismiss('cancel');
};
});

模态 HTML:

<div class="modal-header">
<h3 class="modal-title"><i class="fa fa-exclamation-triangle"></i> Confirm</h3>
</div>
<div class="modal-body">
Modal Text here.....
</div>
<div class="modal-footer">
<button class="btn btn-danger" ng-click="confirmmodalCtrl.ok()">OK</button>
<button class="btn btn-default" ng-click="confirmmodalCtrl.cancel()">Cancel</button>
</div>

关于javascript - 从 javascript 打开 Bootstrap 模式对话框不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29943896/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com