Not able to close modal window by function - angularjs

I am using modal like
<modal title="Success" id='successmessage' visible="successmessage">
<form role="form">
Success ful result.
<button type="submit" class="btn btn-primary" ng-click="closesuccessmessage()" >Ok</button>
</form>
</modal>
I have a directive on modal..
'use strict';
angular.module('abc.directives', [])
.directive('modal', function () {
return {
template: '<div class="modal fade">' +
'<div class="modal-dialog">' +
'<div class="modal-content">' +
'<div class="modal-header">' +
'<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>' +
'<h4 class="modal-title">{{ modalTitle }}</h4>' +
'</div>' +
'<div class="modal-body" ng-transclude></div>' +
'</div>' +
'</div>' +
'</div>',
restrict: 'E',
transclude: true,
replace:true,
scope:true,
link: function postLink(scope, element, attrs) {
scope.modalTitle = attrs.title;
scope.$watch(attrs.visible, function(value){
if(value == true)
$(element).modal('show');
else
$(element).modal('hide');
});
$(element).on('shown.bs.modal', function(){
scope.$apply(function(){
scope.$parent[attrs.visible] = true;
});
});
$(element).on('hidden.bs.modal', function(){
scope.$apply(function(){
scope.$parent[attrs.visible] = false;
});
});
}
};
});
in my angular controller i have created function like below
$scope.closesuccessmessage = function()
{
$scope.successmessage = false;
}
But it does not have any effect. How can i make it work. As it is very important for me to control this from a function. Modal opens up on true value, but does not close on false value.

In order for angularjs to bind variable to view outside of properties expecting expressions you must use brackets.
<modal title="Success" id='successmessage' visible="{{successmessage}}">
<form role="form">
Success ful result.
<button type="submit" class="btn btn-primary" ng-click="closesuccessmessage()" >Ok</button>
</form>
</modal>
However I'm not sure visible is a valid property in HTML. If you want to hide modal using variable you could use ng-show and set success message variable in controller
<modal title="Success" id='successmessage' ng-show="successmessage">
<form role="form">
Success ful result.
<button type="submit" class="btn btn-primary" ng-click="closesuccessmessage()" >Ok</button>
</form>
</modal>

You can't use like above you're doing. You should call modal hide function to hide a modal. Try below snippet
$scope.closesuccessmessage = function()
{
$("#successmessage").modal('hide');
}
EDIT
It's not like that you can't use scope values in your HTML. If your controller is same, you can access it easily. I changed your HTML. Please try below one
<modal title="Success" id='successmessage' data-ng-show="successmessage">
instead
<modal title="Success" id='successmessage' visible="successmessage">

Related

Unable to clear the form after submitting and showing the previously entered data using angular js

I had one textarea and a button beside it.When i click the button a modal window opens,which contains a textarea.I will enter some message and press submit,then the message should be displayed in textarea which is outside the modal window,which iam able to do.But when again i click on button,it is still showing the previously entered message.I'm unable to clear that even after using a clear function for ng-submit.I appreciate some one to help me.
angular.module('ui-rangeSlider', [])
.directive('modal', function () {
return {
template: '<div class="modal fade">' +
'<div class="modal-dialog">' +
'<div class="modal-content">' +
'<div class="modal-header">' +
'<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>' +
'<h4 class="modal-title">{{ title }}</h4>' +
'</div>' +
'<div class="modal-body" ng-transclude></div>' +
'</div>' +
'</div>' +
'</div>',
restrict: 'E',
transclude: true,
replace:true,
scope:true,
link: function postLink(scope, element, attrs) {
scope.title = attrs.title;
scope.$watch(attrs.visible, function(value){
if(value == true)
$(element).modal('show');
else
$(element).modal('hide');
});
$(element).on('shown.bs.modal', function(){
scope.$apply(function(){
scope.$parent[attrs.visible] = true;
});
});
$(element).on('hidden.bs.modal', function(){
scope.$apply(function(){
scope.$parent[attrs.visible] = false;
});
});
}
};
})
var mymodal = angular.module('mymodal', ['ui-rangeSlider']);
mymodal.controller('MainCtrl', function ($scope) {
$scope.showModal = false;
$scope.toggleModal = function(){
$scope.showModal = !$scope.showModal;
};
$scope.submitToggle = true;
//default get the data
//send post, saving to localStorage
$scope.sendPost = function(test) {
//setting the data to the textara
$scope.hello = test;
$scope.hello1 = test;
$scope.clear();
/* //saving the data
return localstore.setData(test);
*/
};
$scope.clear=function(){
$scope.test = null;
};
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet" />
<body ng-app="mymodal">
<div ng-controller="MainCtrl" class="container">
<textarea ng-model="hello">{{test}}</textarea>
<button ng-click="toggleModal()" class="btn btn-default">Open modal</button>
<modal title="Login form" visible="showModal">
<form>
<textarea ng-model="test" class="form-control" id="text" name="modaltext" placeholder="Type in your message" rows="5" maxlength="200"></textarea>
<button class="btn btn-default" type="submit" ng-click="sendPost(test);clear()" data-dismiss="modal">Submit</button>
<div id="textarea_feedback"></div>
</form>
</modal>
</div>
</body>
/http://jsfiddle.net/RLQhh/5562/
You can access and clear the test text when calling the function to open the modal, like this:
scope.$$nextSibling.test = '';
http://jsfiddle.net/RLQhh/5632/
Edit:
Now it also resets the remaining characters to 99.

Modal Popup not working

Good day! I am making a popup on my save button but it seems to be not working. I copied the code on JSFiddle sample but when I run it on my laptop its not working
Here is the result on my browser
here is my code
<body>
<div ng-controller="MainCtrl" class="container">
<button ng-click="toggleModal()" class="btn btn-default">Save</button>
<modal title="Saved!" visible="showModal">
<form role="form">
<p>
Time Settings Succesfully Saved!
</p>
<button type="submit" class="btn btn-default" visible="closeModal">Ok</button>
</form>
</modal>
</div>
<script>
var mymodal = angular.module('mymodal', []);
mymodal.controller('MainCtrl', function ($scope) {
$scope.showModal = false;
$scope.toggleModal = function(){
$scope.showModal = !$scope.showModal;
};
});
mymodal.directive('modal', function () {
return {
template: '<div class="modal fade">' +
'<div class="modal-dialog">' +
'<div class="modal-content">' +
'<div class="modal-header">' +
'<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>' +
'<h4 class="modal-title">{{ title }}</h4>' +
'</div>' +
'<div class="modal-body" ng-transclude></div>' +
'</div>' +
'</div>' +
'</div>',
restrict: 'E',
transclude: true,
replace:true,
scope:true,
link: function postLink(scope, element, attrs) {
scope.title = attrs.title;
scope.$watch(attrs.visible, function(value){
if(value == true)
$(element).modal('show');
else
$(element).modal('hide');
});
$(element).on('shown.bs.modal', function(){
scope.$apply(function(){
scope.$parent[attrs.visible] = true;
});
});
$(element).on('hidden.bs.modal', function(){
scope.$apply(function(){
scope.$parent[attrs.visible] = false;
});
});
}
};
});
</script>
</body>

Html not being rendered in bootstrap popup when used with AngularJS scoped variable

In this fiddle :
http://jsfiddle.net/RLQhh/2695/
If click "Open Modal" the text "this is a new line" appears over two lines.
However If instead use this fiddle :
http://jsfiddle.net/RLQhh/2694/
The text "this is a <br> new line" appears over one line. The <br> element is not being rendered as html when used with a scoped variable.
I have set data-html="true" on both examples.
Why is this occurring ? How to render html when being applied using a scoped variable ?
fiddle src :
<div ng-controller="MainCtrl" class="container">
<h1>Modal example</h1>
<button ng-click="toggleModal()" class="btn btn-default">Open modal</button>
<modal title="Login form" visible="showModal">
<form role="form">
<div class="form-group">
<label data-html="true">{{text}}</label>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</modal>
</div>
var mymodal = angular.module('mymodal', []);
mymodal.controller('MainCtrl', function ($scope) {
$scope.showModal = false;
$scope.text = "this is a <br> new line";
$scope.toggleModal = function(){
$scope.showModal = !$scope.showModal;
};
});
mymodal.directive('modal', function () {
return {
template: '<div class="modal fade">' +
'<div class="modal-dialog">' +
'<div class="modal-content">' +
'<div class="modal-header">' +
'<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>' +
'<h4 class="modal-title">{{ title }}</h4>' +
'</div>' +
'<div class="modal-body" ng-transclude></div>' +
'</div>' +
'</div>' +
'</div>',
restrict: 'E',
transclude: true,
replace:true,
scope:true,
link: function postLink(scope, element, attrs) {
scope.title = attrs.title;
scope.$watch(attrs.visible, function(value){
if(value == true)
$(element).modal('show');
else
$(element).modal('hide');
});
$(element).on('shown.bs.modal', function(){
scope.$apply(function(){
scope.$parent[attrs.visible] = true;
});
});
$(element).on('hidden.bs.modal', function(){
scope.$apply(function(){
scope.$parent[attrs.visible] = false;
});
});
}
};
});

AngularJS reusable modal bootstrap directive

I'm new with AngularJS. I'm trying to implement a reusable modal Bootstrap.
This is the index.html:
<div ng-controller="mymodalcontroller">
<modal lolo="modal1" modal-body='body' modal-footer='footer' modal-header='header' data-ng-click="myRightButton()"></modal>
Launch Demo Modal
</div>
This is the module, controller and directive:
var myModal = angular.module('myModal', []);
myModal.controller('mymodalcontroller', function ($scope) {
$scope.header = 'Put here your header';
$scope.body = 'Put here your body';
$scope.footer = 'Put here your footer';
$scope.myRightButton = function (bool) {
alert('!!! first function call!');
};
});
myModal.directive('modal', function () {
return {
restrict: 'EA',
scope: {
title: '=modalTitle',
header: '=modalHeader',
body: '=modalBody',
footer: '=modalFooter',
callbackbuttonleft: '&ngClickLeftButton',
callbackbuttonright: '&ngClick',
handler: '=lolo'
},
templateUrl: 'partialmodal.html',
transclude: true,
controller: function ($scope) {
$scope.handler = 'pop';
},
};
});
And this is the html template:
<div id="{{handler}}" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">{{header}}</h4>
</div>
<div class="modal-body">
<p class="text-warning">{{body}}</p>
</div>
<div class="modal-footer">
<p class="text-left">{{footer}}</p>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" data-ng-click="callbackbuttonright(), $event.stopPropagation()">Save changes</button>
</div>
</div>
</div>
</div>
I want the 'Launch Alert' button (in the modal) executes the alert and it does it well. The problem is that it is launched when clicking the 'Cancel' button in the Modal and when the window closes. Any ideas?
Here is the working code:CodeThank you.
I would suggest you not bind to ng-click. It does some other magic stuff that can screw with things. There is also a syntax error in your partial.
I've fixed those issues in my fork here:
http://plnkr.co/edit/2jK2GFcKSiKgMQMynD1R?p=preview
To summarize:
script.js:
Change your callbackbuttonright binding from ngClick to ngClickRightButton
myModal.directive('modal', function () {
return {
restrict: 'EA',
scope: {
title: '=modalTitle',
header: '=modalHeader',
body: '=modalBody',
footer: '=modalFooter',
callbackbuttonleft: '&ngClickLeftButton',
callbackbuttonright: '&ngClickRightButton',
handler: '=lolo'
},
templateUrl: 'partialmodal.html',
transclude: true,
controller: function ($scope) {
$scope.handler = 'pop';
},
};
});
index.html:
Change data-ng-click to data-ng-click-right-button
<modal lolo="modal1" modal-body="body" modal-footer="footer" modal-header="header" data-ng-click-right-button="myRightButton()"></modal>
Another minor issue:
partialmodal.html:
Change , to ;
<button type="button" class="btn btn-primary" data-ng-click="callbackbuttonright(); $event.stopPropagation()">Launch Alert</button>
If anyone is still interested, here is a example I recently worked on with bootstrap modal and angularjs directive.
HTML:
<modal visible="showModal1" on-sown="modalOneShown()" on-hide="modalOneHide()">
<modal-header title="Modal Titel 1"></modal-header>
<modal-body>
<h3>This is modal body</h3>
</modal-body>
<modal-footer>
<button class="btn btn-primary" ng-click="hide(1)">Save</button>
</modal-footer>
</modal>
JavaScript:
var myModalApp = angular.module('myModalApp',[]);
myModalApp.directive('modal', function(){
return {
template: '<div class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true"><div class="modal-dialog modal-sm"><div class="modal-content" ng-transclude><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button><h4 class="modal-title" id="myModalLabel">Modal title</h4></div></div></div></div>',
restrict: 'E',
transclude: true,
replace:true,
scope:{visible:'=', onSown:'&', onHide:'&'},
link:function postLink(scope, element, attrs){
$(element).modal({
show: false,
keyboard: attrs.keyboard,
backdrop: attrs.backdrop
});
scope.$watch(function(){return scope.visible;}, function(value){
if(value == true){
$(element).modal('show');
}else{
$(element).modal('hide');
}
});
$(element).on('shown.bs.modal', function(){
scope.$apply(function(){
scope.$parent[attrs.visible] = true;
});
});
$(element).on('shown.bs.modal', function(){
scope.$apply(function(){
scope.onSown({});
});
});
$(element).on('hidden.bs.modal', function(){
scope.$apply(function(){
scope.$parent[attrs.visible] = false;
});
});
$(element).on('hidden.bs.modal', function(){
scope.$apply(function(){
scope.onHide({});
});
});
}
};
}
);
myModalApp.directive('modalHeader', function(){
return {
template:'<div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button><h4 class="modal-title">{{title}}</h4></div>',
replace:true,
restrict: 'E',
scope: {title:'#'}
};
});
myModalApp.directive('modalBody', function(){
return {
template:'<div class="modal-body" ng-transclude></div>',
replace:true,
restrict: 'E',
transclude: true
};
});
myModalApp.directive('modalFooter', function(){
return {
template:'<div class="modal-footer" ng-transclude></div>',
replace:true,
restrict: 'E',
transclude: true
};
});
function ModalController($scope){
$scope.title = "Angularjs Bootstrap Modal Directive Example";
$scope.showModal1 = false;
$scope.showModal2 = false;
$scope.hide = function(m){
if(m === 1){
$scope.showModal1 = false;
}else{
$scope.showModal2 = false;
}
}
$scope.modalOneShown = function(){
console.log('model one shown');
}
$scope.modalOneHide = function(){
console.log('model one hidden');
}
}
Compared to other options, below given the minimalist approach using Angular Bootstrap and an angular factory. See a sample snippet below.
Reusable modal view - ConfirmationBox.html
<div class="modal-header">
<h3 class="modal-title">{{title}}</h3>
</div>
<div class="modal-body">
{{message}}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary btn-warn" data-ng-click="ok(); $event.stopPropagation()">OK</button>
<button type="button" class="btn btn-default" data-ng-click="cancel(); $event.stopPropagation()">Cancel</button>
</div>
Reusable module and shared factory, for handling the reusable modal dialog
angular.module('sharedmodule',['ui.bootstrap', 'ui.bootstrap.tpls'])
.factory("sharedService",["$q", "$modal", function ($q, $modal)
{
var _showConfirmDialog = function (title, message)
{
var defer = $q.defer();
var modalInstance = $modal.open({
animation: true,
size: "sm",
templateUrl: 'ConfirmationBox.html',
controller: function ($scope, $modalInstance)
{
$scope.title = title;
$scope.message = message;
$scope.ok = function ()
{
modalInstance.close();
defer.resolve();
};
$scope.cancel = function ()
{
$modalInstance.dismiss();
defer.reject();
};
}
});
return defer.promise;
}
return {
showConfirmDialog: _showConfirmDialog
};
}]);
Portion of your View, using the shared modal dialog
<a data-ng-click="showConfirm()">Go Back to previous page</a>
Controller of your view, opening your shared reusable modal dialog and handling notifications (Ok and Cancel)
var myModule = angular.module("mymodule", ['sharedmodule', 'ui.bootstrap', 'ui.bootstrap.tpls']);
myModule.controller('myController', ["$scope", "sharedService", "$window",
function ($scope, sharedService, $window)
{
$scope.showConfirm = function ()
{
sharedService.showConfirmDialog(
'Confirm!',
'Any unsaved edit will be discarded. Are you sure to navigate back?')
.then(function ()
{
$window.location = '#/';
},
function ()
{
});
};
}]);

AngularJS custom directive initial loading not working

You can see the working example here - or should I say, non-working example.
Basically, I'm trying to make a custom directive called yesno-group. It is a shortcut to do this over and over:
<div class="btn-group">
<button type="button" class="btn btn-default" ng-model="checkboxField" btn-radio="true">Yes</button>
<button type="button" class="btn btn-default" ng-model="checkboxField" btn-radio="false">No</button>
</div>
And here is my yesno-group directive:
myApp.directive('yesnoGroup', function () {
return {
restrict: 'A',
scope: {
field: '=',
buttonClass: '#'
},
replace: true,
template: '<div class="btn-group">' +
' <button type="button" class="{{buttonClass}}" ng-model="field" btn-radio="true">Yes</button>' +
' <button type="button" class="{{buttonClass}}" ng-model="field" btn-radio="false">No</button>' +
'</div>'
};
});
The problem is, yesno-group does not show the value on the initial load. But once you start changing the value, it gets synchronized with the ngModel field.
What am I missing?
Also, how can I make the yesno-group to accept ng-model and use it instead of field? I got that from JsFiddle-Examples - currency input, but wish to use ng-model unless it is a big hassle.
Thank you!
You need to use ng-class, not class.
http://jsfiddle.net/KT6Zd/12/
myApp.directive('yesnoGroup', function () {
return {
restrict: 'A',
scope: {
field: '=',
buttonClass: '#'
},
replace: true,
template: '<div class="btn-group">' +
' <button type="button" ng-class="buttonClass" ng-model="field" btn-radio="true">Yes</button>' +
' <button type="button" ng-class="buttonClass" ng-model="field" btn-radio="false">No</button>' +
'</div>'
};
});

Resources