angularjs - Opening Bootstrap modal dialog from javascript not working -
can tell why modal dialog not opening on button click? error message: "document.getelementbyid(...).modal not function". please :)
here 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>
and here javasript function open():
app = angular.module('modaldemo', []); app.controller('myctrl', ['$scope', function($scope) { $scope.open = function() { document.getelementbyid('mymodal').modal({ show: true, backdrop: false, keyboard: false }); }; }]);
everything works fine if replace button tag this:
<button type="button" class="btn btn-default" data-toggle="modal" data-target="#mymodal" data-backdrop="static" data-keyboard="false" >
but need open modal javasript function, why it's not working?
try plunker: http://plnkr.co/edit/ppaggfjbe8qcau80lnm5?p=preview
this keeps modal usage inline angular way.
hope works you!
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 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'); }; });
modal 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>
Comments
Post a Comment