ng-click not firing on modal panel angularjs v 1.3.17 - javascript

I must be missing something here. I have a module like below:
(function (app) {
'use strict';
app.controller('modalCtrl', modalCtrl);
modalCtrl.$inject = ['$scope', '$modalInstance'];
function modalCtrl($scope, $modalInstance) {
$scope.closeModal = closeModal;
function closeModal() {
$modalInstance.dismiss();
}
}
})(angular.module('mainModule'));
I call the closeModal function from this vew:
<div class="panel panel-primary">
<div class="panel-heading">
Heading!
</div>
<div class="panel-body">
Some message.
</div>
<div class="panel-footer clearfix">
<div class="pull-right">
<button type="button" class="btn btn-danger" ng-click="closeModal()">OK</button>
</div>
</div>
</div>
Modal opens as expected, however it cannot be closed by clicking ok button. The code opening modal is below:
function openDialog() {
$modal.open({
templateUrl: 'modal.html',
controller: 'modalCtrl',
scope: $scope
}).result.then(function ($scope) {
//some code here
}, function () {
});
}
EDIT:
openDialog function is in different controller than closeModal. Nothing seem to work below. To me it appears as if ng-click on closeModal doesn't find its controller to invoke closeModal, however there is no error thrown for this.

Try opening the modal like this:
function openDialog() {
$modal.open({
templateUrl: 'modal.html',
controller: 'modalCtrl',
scope: this.$new()
}).result.then(function ($scope) {
//some code here
}, function () {
});
}

Create a instance of your modal first
function openDialog() {
$scope.modal = $modal.open({
templateUrl: 'modal.html',
controller: 'modalCtrl',
scope: $scope
}).result.then(function ($scope) {
//some code here
}, function () {
});
}
function closeModal() {
$scope.modal.dismiss();//or close();
}

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

Did you try using the data-dismiss="modal" attribute?

remove scope:$scope from openDialg function
function openDialog() {
$modal.open({
templateUrl: 'modal.html',
controller: 'modalCtrl'
}).result.then(function ($scope) {
//some code here
}, function () {
});
}

Related

AngularJS Bootstrap Modal $modalInstance.dismiss is not a function

When I click the cancel button on my modal, the $modalInstance.dismiss function binded with ng-click on my modal template isn't working.
The console has been throwing the error: "$modalInstance.dismiss is not a function"
MODAL TEMPLATE:
<div class="my-modal ng-scope" id="my-modal">
<div class="modal-header">
<h3 class="modal-title" id="modal-title">Create a new room</h3>
</div>
<div class="modal-body" id="modal-body">
<form>
Enter a room name<br>
<input type="text" name="new-room-name">
</form>
<div class="modal-footer">
<button class="btn btn-warning" type="button" ng-click="modal.cancel()">Cancel</button>
<button class="btn btn-primary" type="button" ng-click="modal.save()">Create Room</button>
</div>
</div>
MAIN CONTROLLER:
(function() {
function HomeCtrl(Room, $scope, $uibModal, $log, $document) {
var home = this;
home.chatRooms = Room.all;
//TO TEST ADD METHOD FROM ROOM.JS
// this.addRoom = Room.add();
home.open = function () {
modalInstance = $uibModal.open({
animation: true,
backdrop: true,
templateUrl: '../templates/modal.html',
controller: 'ModalInstanceCtrl',
controllerAs: 'modal',
bindToContoller: true,
scope: $scope,
size: 'lg',
resolve: {
'$modalInstance': function () { return function () { return modalInstance; } }
}
});
console.log(modalInstance);
modalInstance.result.then(function (newChatRoom) {
home.selected = newChatRoom;
console.log(newChatRoom);
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
};
}
angular
.module('blocChat')
controller('HomeCtrl', ['Room', '$scope', '$uibModal', '$log', '$document', HomeCtrl]);
})();
MODAL CONTROLLER:
(function() {
function ModalInstanceCtrl(Room, $scope, $modalInstance, $log, $document) {
var modal = this;
this.save = function() {
$modalInstance.close(newChatRoom);
};
this.cancel = function() {
$modalInstance.dismiss('cancel');
};
}
angular
.module('blocChat')
.controller('ModalInstanceCtrl', ['Room', '$scope', '$modalInstance', '$log', '$document', ModalInstanceCtrl]);
})();
I've spent about 3 hours messing around with my code, looking at the AngularJS Bootstrap UI documentation, several StackOverflow threads, and other sites and have gotten no where. Any help would be appreciated.
In the version of angular ui bootstrap you're using, the reference to the modal instance is called $uibModalInstance. So try changing your controller to this:
(function() {
function ModalInstanceCtrl(Room, $scope, $uibModalInstance, $log, $document)
{
var modal = this;
this.save = function() {
$uibModalInstance.close(newChatRoom);
};
this.cancel = function() {
$uibModalInstance.dismiss('cancel');
};
}
angular
.module('blocChat')
.controller('ModalInstanceCtrl', ['Room', '$scope', '$uibModalInstance', '$log', '$document', ModalInstanceCtrl]);
})();

Issue with the controller of a UI Bootstrap Modal

I am new to Angular. I have created a component with a template (panel.html) and a controller (allPanelsRetrieved). When a specific button defined in the template is clicked, the showDetails() function specified in the controller is called. This function triggers the opening of a modal dialog specified by a template (panel-list.details-template.html) and a controller, which are both defined inside the allPanelsRetrieved controller. The problem is that the modal dialog is displayed but the controller doesn't work (the click of the OK button does nothing). Where the problem may be? Thanks in advance
angular.
module('panelList')
.component('panelList', {
templateUrl: '/panel-list/panel.html',
controller: ['Panel', 'NgTableParams','$scope', '$location', '$uibModal',
function PanelListController(Panel, NgTableParams, $scope, $location, $uibModal) {
this.allPanelsRetrieved = (index, before) => {
//..hidden code here
}
this.showDetails = function () {
$uibModal.open({
templateUrl: '/panel-list/panel-list.details-template.html',
controller: function ($uibModalInstance) {
let $ctrl = this;
$ctrl.ok = function () {
$uibModalInstance.close();
};
},
}).result.then(
function (success) {
alert(success);
},
function (error) {
alert(error);
}
);
};
}]
});
Here is the template panel-list.details-template.html:
<div>
<script type="text/ng-template" id="/panel-list/panel-list.details-template.html">
<div class="modal-header">
<h3 class="modal-title">Modal title</h3>
</div>
<div class="modal-body">
Modal content
</div>
<div class="modal-footer">
<button class="btn btn-primary" ng-click="$ctrl.ok()">Close</button>
</div>
</script>
Use $scope to make it work in the AngularJS way. Here is a working plnkr demo of what you try to achieve.
$uibModal.open({
templateUrl: '/panel-list/panel-list.details-template.html',
controller: function ($uibModalInstance, $scope) {
$scope.ok = function () {
$uibModalInstance.close();
};
}
}).result.then(
function (success) {
alert(success);
},
function (error) {
alert(error);
}
);
View
<script type="text/ng-template" id="/panel-list/panel-list.details-template.html">
<div class="modal-header">
<h3 class="modal-title">Modal title</h3>
</div>
<div class="modal-body">
Modal content
</div>
<div class="modal-footer">
<button class="btn btn-primary" ng-click="ok()">Close</button>
</div>
</script>
While the angular.component implementation provides a default for the controllerAs syntax...
controllerAs: identifierForController(options.controller) || options.controllerAs || '$ctrl',
...it appears that angular-ui-bootstrap does not.
Simply provide a value for controllerAs in your $uibModal options to continue using the preferred syntax.
$uibModal.open({
templateUrl: '/panel-list/panel-list.details-template.html',
controllerAs: '$ctrl',
controller: function($uibModalInstance) {
let $ctrl = this;
$ctrl.ok = function () {
$uibModalInstance.close();
}
}
http://plnkr.co/edit/q1arN18553XoUZEuPcPl?p=preview
The problem is with this keyword as you are using it. Use arrow function instead and it will work:
$uibModal.open({
templateUrl: '/panel-list/panel-list.details-template.html',
controller: ($uibModalInstance) => {
let $ctrl = this;
$ctrl.ok = function () {
$uibModalInstance.close();
};
},
})

Angular UI Bootstrap modal callback

I have a bootstrap modal which has a single ok and cancel button wired. What I need help with is I need a different instance of ok and cancel for every modal instance that is called.
Modal.html
<div ng-controller="dealerController">
<div class="modelstitle">
<div class="modal-header mdlheader">
<button type="button" class="close close-btn" ng-click="cancel()">×</button>
<h3 class="modal-title mdltitle" ng-model="modalTitle">{{modalTitle}}</h3>
</div>
<div class="modal-body mdlbody">
<p ng-model="modalContent">{{modalContent}}</p><br/>
</div>
<div class="modal-footer footerbtn">
<button class="btn btn-primary btnwarning" type="button" ng-click="cancel()">Cancel</button>
<button class="btn btn-primary btnwarning" type="button" ng-click="ok()">OK</button>
</div>
</div>
Controller
var myApp=angular.module('home', ['ui.bootstrap']);
myApp.controller('ModalController',function($scope,$uibModal){
$scope.showModal = function(){
$scope.modalInstance = $uibModal.open({
animation: true,
templateUrl: 'modal.html',
//controller: 'ModalController',
size: 'sm',
scope: $scope,
// Prevent closing by clicking outside or escape button.
backdrop: 'static',
keyboard: false
});
}
$scope.ok = function(){
$scope.modalInstance.dismiss();
}
$scope.cancel = function() {
//alert("Cancel from main controller");
$scope.modalInstance.dismiss('cancel');
};
})
Plunker
Expected: I need a different ok and cancel implementation for every instance of a modal.
You could make your modal opening logic more generic and pass ok and cancel functions to it and use those in modal's controller.
<button ng-click="showSomeModal()">Modal 1</button>
<button ng-click="showOtherModal()">Modal 2</button>
angular.module('home', ['ui.bootstrap'])
.controller('ModalController',function($scope,$uibModal){
function openModal(ok, cancel) {
$uibModal.open({
animation: true,
templateUrl: 'modal.html',
size: 'sm',
backdrop: 'static',
keyboard: false,
controller: function($scope) {
$scope.ok = function() {
$scope.$close();
ok();
};
$scope.cancel = function() {
$scope.$dismiss();
cancel();
};
}
});
}
angular.extend($scope, {
modalTitle: 'I am a Title',
modalContent: 'I am model content',
showSomeModal: function() {
openModal(angular.noop, angular.noop);
},
showOtherModal: function() {
function ok() {
console.log('ok');
}
function cancel() {
console.log('cancel');
}
openModal(ok, cancel);
}
});
});
Related plunker here https://plnkr.co/edit/6ecoHA
Or instead of using controller, you could use those same functions after modal is closed/dismissed, using modal result.
function openModal(ok, cancel) {
$uibModal.open({
// remove controller altogether
...
})
.result
.then(ok) // closed
.catch(cancel); // dismissed
}
// changes in modal template
<button type="button" ng-click="$close()">OK</button>
<button type="button" ng-click="$dismiss()">Cancel</button>
You can try add extra parametr to you modal instance that will identity your actual opened modal. Then in ok and cancel function do if statement.
var myApp=angular.module('home', ['ui.bootstrap']);
myApp.controller('ModalController',function($scope,$uibModal){
$scope.showModal = function(modalId){
$scope.modalInstance = $uibModal.open({
animation: true,
templateUrl: 'modal.html',
//controller: 'ModalController',
size: 'sm',
scope: $scope,
// Prevent closing by clicking outside or escape button.
backdrop: 'static',
keyboard: false,
});
$scope.modalInstance.modalNameProperty = modalId ;
}
$scope.ok = function(){
if( $scope.modalInstance.modalNameProperty == somename){
//do somethink
}
else if($scope.modalInstance.modalNameProperty == somename)
{
// do other staff
}
$scope.modalInstance.dismiss();
}
The same in cancel function.

Use UI-Bootstrap Modal with Angular-Fullstack and ES6

I would like to use the ui-bootsrap Modal with the AngularJS Fullstack Generator & ES6, but it doesn't work.
I would like to choose a Project, click "Edit" and edit the Project in a large Modal. But I don't get the modal to open.
In the Console, I get this error Message:
"Error: $modal is not defined"
My Project Controller looks like this:
'use strict';
(function() {
class ProjectCtrl {
constructor(Project, $modal, $log) {
this.project = Project;
this.projects = [];
this.getAllProjects(Project);
this.$modal = $modal;
this.log = $log;
}
// Open a modal window to Update a single Project
modalUpdate(size, selectedProject) {
var modalInstance = $modal.open({
templateUrl: 'app/project/project-edit.modal.html',
controller: function ($scope, modalInstance, aProject) {
$scope.project = aProject;
},
size: size,
resolve: {
aProject: function () {
return selectedProject;
}
}
});
modalInstance.result.then(function (selectedItem) {
this.selected = selectedItem;
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
};
}
angular.module('projectApp')
.controller('ProjectCtrl', ProjectCtrl);
})();
app.js looks like this:
'use strict';
angular.module('projectApp', [
'projectApp.constants',
'ngCookies',
'ngResource',
'ngSanitize',
'ui.router',
'ui.bootstrap'
])
.config(function($urlRouterProvider, $locationProvider) {
$urlRouterProvider
.otherwise('/');
$locationProvider.html5Mode(true);
});
The Button to open the Modal:
<button type="button" class="btn btn-default btn-xs pull-right" ng-click="ProjectCtrl.modalUpdate('lg', project)"><span aria-hidden="false"></span> Edit</button>
My guess is that in ProjectCtrl, I have somehow to $inject 'Project', '$modal' & '$log', but I don't know how and where.
I solved the Modal problem in another way, cause it seems that UI Bootstrap doesn't work in ES6. At least not now.
I used the Modal Service here for Angular-Fullstack.
Here is a simple example with a delete Modal:
'use strict';
(function() {
class AdminController {
constructor(User, Modal) {
// Use the User $resource to fetch all users
this.users = User.query();
this.modal = Modal;
}
delete(user) {
var deleteConfirmationModal = this.modal.confirm.delete((user) => {
user.$remove();
this.users.splice(this.users.indexOf(user), 1);
});
deleteConfirmationModal(user.name, user);
}
}
angular.module('sampleApp.admin')
.controller('AdminController', AdminController);
})();
This is how the Modal looks like:
Which version of ui-bootstrap are you using ?
The syntax is different depending on which version you're using.
Assuming you're using v0.13.4 or older and without ES6, here the solution :
NOTE :
If you're using more recent version than v0.13.4 then replace $modal by $uibModal and $modalInstance by $uibModalInstance and it should work too.
First, what you need to do is write in the command line :
yo angular-fullstack:controller MyModalName
then in myModalName.controller.js put this :
.controller('MyModalNameCtrl', function ($scope,$modal) {
$scope.openModal = function () {
var modalInstance = $modal.open({
animation: true,
templateUrl: 'testmodal',
controller: 'ModalInstanceCtrl',
});
modalInstance.result.then(function () {}, function () {
console.log('Modal dismissed at: ' + new Date());
});
};
});
Then still in myModalName.controller.js put below :
//change theNameOfYourApp to your corresponding app name !
angular.module('theNameOfYourApp').controller('ModalInstanceCtrl', function ($scope, $modalInstance) {
$scope.ok = function () {
$modalInstance.close();
};
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
};
});
Then last step, in your .html page where you want to display the modal, put :
<!--Modal Template-->
<div ng-controller="MyModalNameCtrl">
<script type="text/ng-template" id="testmodal">
<div class="modal-header">
<h3 class="modal-title">Hello :</h3>
</div>
<div class="modal-body">
<p>hey, this is the body </p>
</div>
<div class="modal-footer">
<button class="btn btn-default" type="button" ng-click="ok()">OK</button>
<button class="btn btn-default" type="button" ng-click="cancel()">Cancel()</button>
</div>
</script>
</div>

Parameter not Resolving in Angular Modal

I'm creating a simple update modal. To do so, I'm passing a parameter into my angular-bootstrap modal. I've two controllers, ModalCtrl and ModalInstanceCtrl:
adminController.controller('ModalCtrl', ['$scope', '$modal', '$log', 'Profile',
function ($scope, $modal, $log, Profile) {
$scope.open = function (profile) {
var modalInstance = $modal.open({
animation: true,
templateUrl: 'myModalContent.html',
controller: 'ModalInstanceCtrl',
resolve: {
profile: function () {
$log.info(profile);
return profile;
}
}
});
modalInstance.result.then(function (updatedProfile) {
$log.info(updatedProfile);
Profile.post(updatedProfile);
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
};
}]);
adminController.controller('ModalInstanceCtrl', function ($scope, $modalInstance, profile) {
$scope.ok = function () {
$modalInstance.close(profile);
};
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
};
});
The profile is logged both when opening and accepting the modal - leading me to believe that it's being passed into the instance without issue. However, displaying the modal doesn't display anything from that profile object:
<script type="text/ng-template" id="myModalContent.html">
<div class="modal-header">
<h3 class="modal-title">Profile View</h3>
</div>
<div class="modal-body">
<div><p>{{profile.profileType}}</p></div>
</div>
<div class="modal-footer">
<button class="btn btn-primary" type="button" ng-click="ok()">OK</button>
<button class="btn btn-warning" type="button" ng-click="cancel()">Cancel</button>
</div>
</script>
What am I doing wrong?
You need to put the profile object on the $scope for it to be accessible in the HTML template:
adminController.controller('ModalInstanceCtrl', function ($scope, $modalInstance, profile) {
$scope.profile = profile;
$scope.ok = function () {
$modalInstance.close(profile);
};
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
};
});

Categories