open modal only when data is true - angularjs

I am working on a script where a modal opens up only after the output from python file says dup_yes is 1
I have the modal as following:
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#dupModal" data-keyboard="false" data-backdrop="static" ng-click="save_me()" >Save Me</button>
<div class="modal fade" id="dupModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<h3 class="modal-alert">Same name already exists</h3>
<h6 class="modal-ask">Save both entries or do you want to cancel?</h6>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal" aria-hidden="true" >Keep Both</button>
<button type="button" class="btn btn-default" data-dismiss="modal" aria-hidden="true" >Close</button>
</div>
</div>
</div>
</div>
and my script is as follows:
$scope.save_me = function () {
http.get("path to python file").then(function(response){
$scope.check=response.data;
if ($scope.check['login_status'] == 1 ){
console.log("Mission is a Success\n");
$scope.dup_yes=1;
}
})
}
When I click on button it opens up the modal and runs the python file simultaneously.
Instead I want it to open up the modal only when my python script gives the value 1.
Any ideas on how to do this?

You can access the modal from the controller and open it like this:
$scope.save_me = function () {
http.get("path to python file").then(function(response){
$scope.check=response.data;
if ($scope.check['login_status'] == 1 ){
console.log("Mission is a Success\n");
$scope.dup_yes=1;
var modal = angular.element('#dupModal');
//modal.modal('hide');
modal.modal('show');
}
})
}
Mayby a better solution is to handle the modal action in a separete controller and inject that into your current controller to keep the responsibilities separate

Related

How to close a bootstrap modal programmatically using AngularJS

I have a tab view which has a button to open a modal.
<button class="btn btn-primary btn-etis" data-toggle="modal" data-target="#addModal">Add</button>
The modal is opened via ng-include and by its referencing its name which is 'addModal'
<div ng-include=" 'register/individual/views/modal/addBookOfAccounts.html' "></div>
Here is my modal code:
<!-- Add BOA Modal-->
<ng-form class="form-etis" name="addBoaForm" method="post">
<div class="modal fade" id="addModal" tabindex="-1" role="dialog"
aria-labelledby="addIdentificationModalLabel" aria-hidden="true"
data-backdrop="static">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<div class="modal-title" id="addModalLabel">
<div class="no-margin no-padding">
<i class="fa fa-book"></i> NEW Book of Account Details
</div>
</div>
</div>
<div class="pull-right">
<a class="btn btn-primary btn-etis" href=""
ng-click="ctrl.submitBoaForm()">Ok</a>
<a class="btn btn-danger btn-etis" href=""
data-dismiss="modal" role="button" ng-click="ctrl.closeBoaForm()">Cancel</a>
</div>
inside my controller
self.submitBoaForm = function () {
if (self.isBoaFormValid()) {
self.bookOfAccountsGrid.data.push(self.bookOfAccounts);
self.resetBoaDetails();
alert('New BOA Saved');
} else {
console.log('invalid boa form')
}
}
self.closeBoaForm = function () {
self.showBoaFormValidations = false;
self.resetBoaDetails();
};
I am able to display the modal just by using data-target. My only problem is on how do i close the modal programmatically inside the submitBoaForm() after the alert method?

How to enhance ngSanitize whitelists?

I'm trying to append HTML content from the controller to the view. For this I know that we have to use ngSanitize. Everything is working fine, but it is stripping out the attributes of the HTML elements.
Here is my controller:
var modalVar = '<div class="modal fade" id="deleteModal'+ data.data.id +'" role="dialog"><div class="modal-dialog"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal">×</button><h4 class="modal-title">Alert</h4></div><div class="modal-body"><p>Are you sure to delete?</p></div><div class="modal-footer"><button type="button" class="btn btn-default" data-dismiss="modal">Close</button><button type="button" class="btn btn-danger" ng-click="deleteFunc('+ data.data.id +')" data-dismiss="modal">Yes</button></div></div></div></div>';
$scope.modalsDiv = $scope.modalsDiv + modalVar;
The view:
<div ng-bind-html="modalsDiv"></div>
If I inspect and see the rendered elements, every div element is rendered but their attributes are stripped out.
Controllers should not be adding HTML to the view.
<!--REPLACE ng-html-bind
<div ng-bind-html="modalsDiv"></div>
-->
<!--WITH ng-if -->
<div ng-if="showModal">
<div class="modal fade" id="deleteModal{{data.data.id}} +'" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">
×
</button>
<h4 class="modal-title">
Alert
</h4>
</div>
<div class="modal-body">
<p>Are you sure to delete?</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">
Close
</button>
<button type="button"
class="btn btn-danger"
ng-click="deleteFunc(data.data.id)"
data-dismiss="modal">
Yes
</button>
</div>
</div>
</div>
</div>
</div>
JS
//$scope.modalsDiv = modalVar;
$scope.showModal = true;
Instead of composing HTML in the controller, use the ng-if directive. The code will be much more readable.
try this in your controller:
someVar = $sce.trustAsHtml('someVar');
do not forget to inject $sce into ctrl

Angular Bootstrap multiple modal backdrops

I'm using this modal service to show a basic dialog in an Angular app.
The implementation is simple but there are two issues:
The dialog shows but the modal-backdrop class is repeated multiple times, rendering it completely black.
When closing the dialog, the form closes but the background remains, leaving the view useless. I'm sure this is related to 1.
I'm using Angular 1.4.9 with angular-ui-bootstrap 0.13.4 [2015-09-03]. I've found a couple of posts showing incompatibilities between angular 1.4 and angular-ui-bootstrap but I've not managed to solve this.
Edit - view and controller code:
<button type="button" class="btn btn-sm btn-white" ng-click="procFileCtrl.viewErrors(file)"> <i class="fa fa-eye"></i> </button>
this.viewErrors = function(file) {
ModalService.showModal({
templateUrl: 'modules/components/client/modal/modal-base.html',
controller: 'FileController',
controllerAs: 'fileCtrl',
inputs: {
data: file
}
}).then(function(modal) {
modal.element.modal();
modal.close.then(function(result) {
this.message = result ? 'ok' : 'notok';
});
}).catch(function(error) {
console.log(error);
});
};
modal-base.html
<div class="modal inmodal in" id="myModal" tabindex="-1" role="dialog" aria-hidden="true" style="display: block;">
<div class="modal-dialog">
<div class="modal-content animated bounceInRight">
<div class="modal-header">
<!-- header here -->
</div>
<div class="modal-body">
<!-- body here -->
</div>
<div class="modal-footer">
<button type="button" class="btn btn-white" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>

AngularJs - Close modal from Controller

I have a modal which i am trying to close from the controller - however i am unable to do so
<button type="button" class="add-entry-btn-group" data-animation="am-fade- and-slide-top" data-template-url="states/../numbersModal.html" bs- modal="modal">New</button>
Modal HTML ->
numbersModal.html -
<div class="modal" id="addNumbersModal" tabindex="-1" role="dialog" data-backdrop="static" aria-labelledby="addNumbersModal"
data-animation="am-fade-and-scale" data-placement="center" >
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<form method="POST" class="form-horizontal group-border-dashed" name="addNumbersForm" novalidate>
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span>×</span></button>
<h4 class="modal-title"> ID Numbers</h4>
</div>
<button class="btn btn-default" type="button" ng-click="closeIdNumbersModal()">
<span class="glyphicon glyphicon-remove"></span> Close</button>
<button type="button" class="btn btn-default" type="reset" ng-click="idNumbers.closeIdNumbersModal()" data-dismiss="modal">Cancel</button>
<button type="submit" ng-click="idNumbers.saveIdNumbers()" class="btn btn-primary">Save</button>
Controller ->
function closeIdNumbersModal() {
//tried all these things - none works
vm.addIdNumbersModal.$promise.then(vm.addIdNumbersModal.hide);
vm.addIdNumbersModal.close;
vm.addIdNumbersModal.hide;
vm.addIdNumbersModal.hide;
}
These look like Bootstrap modal classes.
If so, then you can close them using JQuery (which is a dependency of bootstrap.js):
$('#addNumbersModal').modal('hide');

how can i add controller as a function to angularstrap modal?

in angularstrap documentation controller can be a function. but there is no example for this ability.
I am using this code for creating and display a modal using angularStrap:
$scope.openCheckDialog = function(){
var checkModal= $modal({title :"test 1",
templateUrl:"temp/checkTemp.html",
show:false,
controller:function(){
console.log("show first log to me!!!");
this.test = function(){
console.log("show other log to me") ;
}
}});
checkModal.$promise.then(checkModal.show) ;
};
and it is my modal template script:
<div class="modal" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header" ng-show="title">
<button type="button" class="close" aria-label="Close" ng-click="$hide()"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" ng-bind="title"></h4>
</div>
<div class="modal-body" ng-bind="content"></div>
<div class="modal-footer">
<button type="button" class="btn btn-default" ng-click="$hide()">Close</button>
<button type="button" class="btn btn-primary" ng-click="test()">Save</button>
</div>
</div>
</div>
</div>
when I open this dialog first log is shown in console but when I click on save button no thing happen.
How about this?
var checkModal= $modal({
title :"test 1",
templateUrl:"temp/checkTemp.html",
show:false,
controller: ['$scope', function($scope){
$scope.test = function () {
console.log("show other log to me");
};
}]
});

Resources