New to Angular and struggling through some modal service work. Currently stuck on a Cannot read property 'showModal' of undefined error when attempting to open the modal window.
modalService
angular.module('cloudPortalApp').service('modalService', ['$modal',
function ($modal) {
var modalDefaults = {
backdrop: true,
keyboard: true,
modalFade: true,
templateUrl: '/Shared/NewCommission.cshtml'
};
var modalOptions = {
closeButtonText: 'Close',
actionButtonText: 'OK',
headerText: 'Proceed?',
bodyText: 'Perform this action?'
};
this.showModal = function (customModalDefaults, customModalOptions) {
if (!customModalDefaults) customModalDefaults = {};
customModalDefaults.backdrop = 'static';
return this.show(customModalDefaults, customModalOptions);
};
this.show = function (customModalDefaults, customModalOptions) {
//Create temp objects to work with since we're in a singleton service
var tempModalDefaults = {};
var tempModalOptions = {};
//Map angular-ui modal custom defaults to modal defaults defined in service
angular.extend(tempModalDefaults, modalDefaults, customModalDefaults);
//Map modal.html $scope custom properties to defaults defined in service
angular.extend(tempModalOptions, modalOptions, customModalOptions);
if (!tempModalDefaults.controller) {
tempModalDefaults.controller = function ($scope, $modalInstance) {
$scope.modalOptions = tempModalOptions;
$scope.modalOptions.ok = function (result) {
$modalInstance.close(result);
};
$scope.modalOptions.close = function (result) {
$modalInstance.dismiss('cancel');
};
}
}
return $modal.open(tempModalDefaults).result;
};
}]);
Controller script:
angular.module("cloudPortalApp").controller('commissionManagerIndexController',['$scope', '$http', '$window', function ($scope, $http, $modal, $log, modalService, dataService) {
$scope.commissionCount = 0;
$scope.showNewCommissionModal = function () {
var modalDefaults = {
backdrop: true,
keyboard: true,
modalFade: true,
templateUrl: '/Views/Shared/NewCommission.cshtml'
}
modalService.showModal(modalDefaults, {}).then(function (result) {
});
}
Html:
#section Scripts {
<script src="~/Scripts/angular-ui/ui-bootstrap.js"></script>
<script src="~/Scripts/angular-ui/ui-bootstrap-tpls.js"></script>
<script src="~/js/modalService.js"></script>
<script src="~/js/commissionManagerIndex.js"></script>
}
<script id="template" type="text/x-kendo-template">
<div class="toolbar">
#*<a class="k-button" href="\#" ng-click="toolbarClick()">Create New Commission</a>*#
<button kendo-button ng-click="edit(this)"><span class="k-icon k-i-tick"></span>Edit</button>
</div>
</script>
<h2>Commission Manager</h2>
<h3>CommissionCount: {{ commissionCount }}</h3>
<div class="text-left">
<button type="button" class="btn btn-primary" ng-click="showNewCommissionModal()">
Create New Commission
</button>
</div>
}]);
Related
I am beginner in AngularJS and I am trying to edit table list object using below code and my modal form having save and cancel buttons.
As per my requirement when I click on save button then only object need to update and if I click on cancel button it should not update but using my below code even when I not tap on save button its automatically updating.
How can I do my requirement? Can some one help me please.
add_user.html
<div>
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">New User Registration</h4>
</div>
<div class="modal-body">
<form class="form-horizontal">
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Username</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputEmail3" placeholder="Username" ng-model="newUser.username">
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputEmail3" placeholder="Email" ng-model="newUser.email">
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Full Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputEmail3" placeholder="Full Name" ng-model="newUser.fullName">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="button" class="btn btn-default" data-dismiss="modal" ng-click="saveUser();">Save</button>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" ng-click="close()">Close</button>
</div>
</div>
app.js
var app = angular.module('myApp', ['ui.bootstrap']);
app.controller('myController', ['$scope', '$uibModal', '$log',
function ($scope, $uibModal, $log) {
$scope.newUser = {};
$scope.info = "";
$scope.users = [
{ username: "rimon", fullName: "Md. Mamunur Rashid Rimon", email: "rimonmath#gmail.com" },
{ username: "shamim", fullName: "Md. Tamim Hossain", email: "shamim#gmail.com" },
{ username: "tamim", fullName: "Tamim Iqbal", email: "tamim#gmail.com" }
];
$scope.addUser = function () {
var modalInstance = $uibModal.open({
templateUrl: 'add_user.html',
controller: 'ModalInstanceCtrl',
resolve: {
items: function () {
return $scope.users;
}
}
});
modalInstance.result.then(function (selectedItem) {
$scope.selected = selectedItem;
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
}
$scope.editUser = function (index) {
var modalInstance = $uibModal.open({
templateUrl: 'add_user.html',
controller: 'EditInstanceCtrl',
resolve: {
user: function () {
var obj = {
arrayList: $scope.users,
position: index
}
return obj;
}
}
});
modalInstance.result.then(function (selectedItem) {
$scope.selected = selectedItem;
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
};
$scope.deleteUser = function () {
console.log($scope.users.indexOf($scope.clickedUser));
$scope.users.splice($scope.users.indexOf($scope.clickedUser), 1);
$scope.info = "User Deleted Successfully!";
};
$scope.clearInfo = function () {
$scope.info = "";
};
}]);
angular.module('myApp').controller('ModalInstanceCtrl', ['$scope', '$uibModalInstance', 'items',
function ($scope, $uibModalInstance, items) {
$scope.saveUser = function () {
$scope.users = items;
$uibModalInstance.close();
$scope.users.push($scope.newUser);
$scope.info = "New User Added Successfully!";
$scope.newUser = {};
};
$scope.close = function () {
$uibModalInstance.dismiss('cancel');
};
}]);
angular.module('myApp').controller('EditInstanceCtrl', ['$scope', '$uibModalInstance', 'user',
function ($scope, $uibModalInstance, user) {
$scope.newUser = user.arrayList[user.position];
$scope.users = user.arrayList;
$scope.saveUser = function () {
$scope.users[user.position] = $scope.newUser;
$uibModalInstance.close();
};
$scope.close = function () {
$uibModalInstance.dismiss('cancel');
};
}]);
You should add/edit your user inside the controller myController, to do so, the modal needs to return the user:
var app = angular.module('myApp', ['ui.bootstrap']);
app.controller('myController', ['$scope', '$uibModal', '$log',
function ($scope, $uibModal, $log) {
$scope.newUser = {};
$scope.info = "";
$scope.users = [
{ username: "rimon", fullName: "Md. Mamunur Rashid Rimon", email: "rimonmath#gmail.com" },
{ username: "shamim", fullName: "Md. Tamim Hossain", email: "shamim#gmail.com" },
{ username: "tamim", fullName: "Tamim Iqbal", email: "tamim#gmail.com" }
];
$scope.addUser = function () {
var modalInstance = $uibModal.open({
templateUrl: 'add_user.html',
controller: 'ModalInstanceCtrl',
resolve: {
items: function () {
return angular.copy($scope.users);
}
}
});
modalInstance.result.then(function (newUser) {
$scope.users.push(newUser);
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
}
$scope.editUser = function (index) {
var modalInstance = $uibModal.open({
templateUrl: 'add_user.html',
controller: 'EditInstanceCtrl',
resolve: {
user: function () {
var obj = {
arrayList: angular.copy($scope.users),
position: index
}
return obj;
}
}
});
modalInstance.result.then(function (user) {
$scope.users[index] = user
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
};
$scope.deleteUser = function () {
console.log($scope.users.indexOf($scope.clickedUser));
$scope.users.splice($scope.users.indexOf($scope.clickedUser), 1);
$scope.info = "User Deleted Successfully!";
};
$scope.clearInfo = function () {
$scope.info = "";
};
}]);
angular.module('myApp').controller('ModalInstanceCtrl', ['$scope', '$uibModalInstance', 'items',
function ($scope, $uibModalInstance, items) {
$scope.saveUser = function () {
$uibModalInstance.close($scope.newUser);
};
$scope.close = function () {
$uibModalInstance.dismiss('cancel');
};
}]);
angular.module('myApp').controller('EditInstanceCtrl', ['$scope', '$uibModalInstance', 'user',
function ($scope, $uibModalInstance, user) {
$scope.newUser = user.arrayList[user.position];
$scope.users = user.arrayList;
$scope.saveUser = function () {
$uibModalInstance.close($scope.newUser);
};
$scope.close = function () {
$uibModalInstance.dismiss('cancel');
};
}]);
The fact that we use angular.copy($scope.users); allows us to make sure the modal won't update your list of user.
This line: $uibModalInstance.close($scope.newUser) returns the value to the controller.
Your list is then updated in the modalInstance.result.then function.
I dont see ng-app being added or the controller being referenced anywhere in the HTML
I'm working on a project that makes sense to add and edit items within a modal window. The add portion is working fine. The edit portion is opening the modal but my ng-model is not reflecting and the input fields are blank within the modal window.
Here's a plunker to illustrate the issue:
http://plnkr.co/edit/8mHOo0YE4qv0UcDvCCgS?p=preview
AngularJS Code:
// Code goes here
var userPopup = angular.module('userPopup', ['ngAnimate', 'ngSanitize', 'ui.bootstrap']);
userPopup.controller('popupController', function($scope, $uibModal, $log) {
$scope.users = [{
name: 'Steve',
job: 'Accounting',
age: '39',
sal: '36000',
addr: '123 Streetly Unit 2, Chicago, IL 60601'
}];
$scope.editUser = function(user) {
var dialogInst = $uibModal.open({
templateUrl: 'edit.html',
controller: 'editDialogInstCtrl',
size: 'lg',
resolve: {
selectedUser: function() {
return $scope.user;
}
}
});
};
$scope.addUser = function() {
var dialogInst = $uibModal.open({
templateUrl: 'popup.html',
controller: 'DialogInstCtrl',
size: 'lg',
resolve: {
selectedUser: function() {
return $scope.user;
}
}
});
dialogInst.result.then(function(newuser) {
$scope.users.push(newuser);
$scope.user = {
name: '',
job: '',
age: '',
sal: '',
addr: ''
};
}, function() {
$log.info('Modal dismissed at: ' + new Date());
});
};
});
userPopup.controller('DialogInstCtrl', function($scope, $uibModalInstance, selectedUser, $log) {
//Assign Selected User
$scope.user = selectedUser;
$scope.submitUser = function() {
$uibModalInstance.close($scope.user);
};
$scope.cancel = function() {
$uibModalInstance.dismiss('cancel');
};
});
userPopup.controller('editDialogInstCtrl', function($scope, $uibModalInstance, selectedUser, $log) {
//Assign Selected User
$scope.user = selectedUser;
$scope.save = function() {
$uibModalInstance.close($scope.user);
};
$scope.cancel = function() {
$uibModalInstance.dismiss('cancel');
};
});
HTML Code:
<a class="btn btn-link" ng-click="editUser(selectedUser)">Edit</a>
I know it's close and I'm overlooking something. Please advise.
thank you
You have two issues, that are just typos, really.
In your JS code:
// passing `user` here
// v
$scope.editUser = function(user) {
var dialogInst = $uibModal.open({
/* ... */
resolve: {
selectedUser: function() {
return $scope.user;
} // ^
} // using $scope.user there
});
};
You're passing an argument to the editUser function, but not actually using it; referring instead to $scope.user, which is undefined.
It should be: selectedUser: function() { return user; }
In your HTML template:
<!-- declaring `user` here -->
<tr ng-repeat="user in users">
<td>{{user.name}}</td>
<td>{{user.job}}</td>
<td>{{user.age}}</td>
<td>{{user.sal | currency}}</td>
<td>{{user.addr}}</td>
<!-- using `selectedUser` there -->
<td><a class="btn btn-link" ng-click="editUser(selectedUser)">Edit</a></td>
</tr>
You're iterating over users, using a user variable; then referring to a (non-existent) selectedUser when calling editUser.
It should be: <a class="btn btn-link" ng-click="editUser(user)">Edit</a>
Here's a link to a (working) fork of your plunk.
You need to change this code in your view.
replace this code
<td><a class="btn btn-link" ng-click="editUser(selectedUser)">Edit</a></td>
to
<td><a class="btn btn-link" ng-click="editUser(user)">Edit</a></td>
and in your controller when invoke the modal.. change
$scope.editUser = function(user) {
var dialogInst = $uibModal.open({
templateUrl: 'edit.html',
controller: 'editDialogInstCtrl',
size: 'lg',
resolve: {
selectedUser: function() {
return $scope.user;
}
}
});
};
to
$scope.editUser = function(user) {
var dialogInst = $uibModal.open({
templateUrl: 'edit.html',
controller: 'editDialogInstCtrl',
size: 'lg',
resolve: {
selectedUser: function() {
return user;
}
}
});
};
I am using angular modal as per the example here http://plnkr.co/edit/bDqAll8CYwu7DGfSs7FC?p=preview
Here is my app.js and controller code
app.js
var app = angular.module("app", ['trNgGrid', 'ngAnimate', 'ngRoute', 'treasure-overlay-spinner', 'ui.bootstrap']);
app.config(['$routeProvider',
function ($routeProvider) {
$routeProvider.
when('/Employee/', {
templateUrl: '/app/views/Employee.html'
}).
otherwise({
redirectTo: '/Employee'
});
}]);
Controller code
angular.module('app').controller('EmployeeController',['$scope','$modal','appService', function ($scope, $modal, appService) {
var employeesList = appService.GetEmployess();
var modalInstance;
$scope.clickMe = function () {
modalInstance = $modal.open(
{
templateUrl: '/app/views/EditEmployee.html',
resolve: {
callerData: function () { }
}
})
modalInstance.result.then(function () { });
}
$scope.cancel = function () {
modalInstance.close('closed result');
};
$scope.spinner = {
active: true
};
employeesList.then(function successCallback(response) {
$scope.Employees = response.data;
}, function errorCallback(response) {
}).finally(function () {
$scope.spinner = {
active: false
};
});
}]);
Here is my HTML code where I am calling controller
<div ng-controller="EmployeeController">
<input type="button" ng-click="clickMe()" value="Click" />
<input type="button" ng-click="cancel()" value="cancel" />
<treasure-overlay-spinner active='spinner.active'>
<table tr-ng-grid='' items='Employees' page-items="10"></table>
</treasure-overlay-spinner>
</div>
I tried as per the example suggested in plunker but not able to hide the modal, so can let me know how can I integrate it correctly
I changed my controller as follows and working as expected now
angular.module('app').controller('EmployeeController', function ($scope, $modal, appService) {
var employeesList = appService.GetEmployess();
$scope.spinner = {
active: true
};
employeesList.then(function successCallback(response) {
$scope.Employees = response.data;
}, function errorCallback(response) {
}).finally(function () {
$scope.spinner = {
active: false
};
});
$scope.open = function (size) {
var modalInstance = $modal.open({
templateUrl: '/app/views/EditEmployee.html',
controller: ModalInstanceCtrl,
size: size,
scope: $scope
});
console.log(modalInstance);
modalInstance.result.then(function (selectedItem) {
console.log('closed :' + selectedItem);
}, function () {
console.log('canceled');
});
};
$scope.otherFunction = function () {
console.log('otherFunction');
//DO SOME STUFF
//....
//THEN CLOSE MODAL HERE
}
});
var ModalInstanceCtrl = function ($scope, $modalInstance) {
$scope.ok = function () {
console.log('var var: ' + $scope.VARCONTROLLER);
$modalInstance.close('closed result');
};
$scope.cancel = function () {
console.log('cancel cancel');
$modalInstance.dismiss('cancel');
};
};
Sorry but I cant see in your code how have you tried to implement the modal view.
In your example, it´s made by this: (HTML:)
<button ng-click="open()" class="btn btn-default">MODAL3</button>
<script type="text/ng-template" id="myModalContent2.html">
<div class="modal-header">
<h3 class="modal-title">I'm a modal3!</h3>
</div>
<div class="modal-body">
<h3>MODAL TITLE</h3>
<button ng-click="otherFunction()" class="btn btn-default">Do stuff</button>
</div>
<div class="modal-footer">
<button class="btn btn-primary" ng-click="ok()">OK</button>
<button class="btn btn-warning" ng-click="close()">Cancel</button>
</div>
</script>
And into controllers (app.js):
$scope.open = function (size) {
var modalInstance = $modal.open({
templateUrl: 'myModalContent2.html',
controller: ModalInstanceCtrl,
size: size,
scope: $scope
});
console.log(modalInstance);
modalInstance.result.then(function (selectedItem) {
console.log('closed :'+selectedItem);
}, function () {
console.log('canceled');
});
};
$scope.otherFunction = function () {
console.log('otherFunction');
//DO SOME STUFF
//....
//THEN CLOSE MODAL HERE
}
EDIT: I see in your controllers how have you tried to use open() and etc to open a modal view, but its not defined (with its script part) into html
EDIT2: THIS IS MY WAY TO USE MODALS: (controllers.js, inside the controller)
$scope.set = function() {
$scope.modal.hide();
}
$scope.cancel = function() {
//this is my function inside modal
$scope.modal.hide();
}
$scope.showModal = false;
$scope.show = function(){
$scope.showModal = !$scope.showModal;
};
$ionicModal.fromTemplateUrl('modal3.html', function(modal) {
$scope.modal = modal;
}, {
animation: 'slide-in-up',
focusFirstInput: true,
scope: $scope
});
In my html , like in the example, under the button to open:
<button class="row header" ng-click="modal.show()">mymodal</button></td>
<script id="modal3.html" type="text/ng-template">
<div class="modal">
*...stuff...*
</div>
</script>
I would like to call dialog's controller function on $modal.rendered event, is this doable without polluting $scope?
Here is a sample Plunkr:
http://plnkr.co/edit/HzFe65RY3hNme8QuUWaJ?p=preview
So, in promise:
modalDialog.rendered.then(function () {
demo.message = 'Dialog opened';
});
I would like to call onLoaded function from modalController controller.
Thank you, best regards,
You can use a factory/service to share data & functions across controllers;
angular.module('app', ['ui.bootstrap'])
.controller('demoController',['$modal', 'modalService', function($modal, service) {
var demo = this;
demo.message = 'It works!'
demo.modal = function() {
var modalDialog = $modal.open({
controller: 'modalController',
controllerAs: 'modal',
templateUrl: 'modal.html'
});
modalDialog.rendered.then(function () {
demo.message = 'Dialog opened';
service.onLoaded();
});
};
}])
.controller('modalController', ['$modalInstance', 'modalService', function($modalInstance, service) {
var modal = this;
service.modalText = 'Modal Text';
modal.shared = service;
modal.cancel = function() {
$modalInstance.dismiss();
}
}])
.factory('modalService', function() {
var service = {
modalText: 'Modal text',
onLoaded: onLoaded
};
return service;
function onLoaded() {
service.modalText = 'Modal loaded';
}
})
And the following update to the html;
<div class="modal-body">
<pre>{{ modal.shared.modalText }}</pre>
</div>
Updated plunker: http://plnkr.co/edit/q704IjkHSAYwuCfLuYnU?p=preview
I want to show a modal box asking the user if they want to delete a post or not. I can't figure out how to pass the key which I pass in the key argument in which I also can alert.
I took the code from the angular twitterbootstrap site but I can't figure out a method to pass data to confirm the remove.
Thanks
Mohammad
$scope.deletePost = function(key, post, event) {
alert(key);
var modalInstance = $modal.open({
templateUrl: 'deletePost.html',
controller: ModalInstanceCtrl,
resolve: {
items: function() {
return $scope.posts;
}
},
})
modalInstance.result.then(function(selectedItem) {
$scope.selected = selectedItem;
alert(selectedItem);
});
};
var ModalInstanceCtrl = function($scope, $modalInstance, items) {
$scope.items = items;
$scope.selected = {
item: $scope.items[0]
};
$scope.ok = function() {
$modalInstance.close($scope.selected.item);
};
$scope.cancel = function() {
$modalInstance.dismiss('cancel');
};
};
Send the key via resolve as a parameter (see plunker):
angular.module('plunker', ['ui.bootstrap']);
var ModalDemoCtrl = function ($scope, $modal, $log) {
var key = 1000;
$scope.items = ['item1', 'item2', 'item3'];
$scope.open = function () {
var modalInstance = $modal.open({
templateUrl: 'myModalContent.html',
controller: ModalInstanceCtrl,
resolve: {
items: function () {
return $scope.items;
},
key: function() {return key; }
}
});
modalInstance.result.then(function (selectedItem) {
$scope.selected = selectedItem;
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
};
};
// Please note that $modalInstance represents a modal window (instance) dependency.
// It is not the same as the $modal service used above.
var ModalInstanceCtrl = function ($scope, $modalInstance, items, key) {
alert("The key is " + key)
$scope.items = items;
$scope.selected = {
item: $scope.items[0]
};
$scope.ok = function () {
$modalInstance.close($scope.selected.item);
};
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
};
};
I would suggest using Bootstrap components written by the AngularUI Team. you can find a great set of Twitter Bootstrap components including Modal control.
Example:
html:
<div ng-controller="ModalDemoCtrl">
<script type="text/ng-template" id="myModalContent.html">
<div class="modal-header">
<h3>I'm a modal!</h3>
</div>
<div class="modal-body">
<ul>
<li ng-repeat="item in items">
<a ng-click="selected.item = item">{{ item }}</a>
</li>
</ul>
Selected: <b>{{ selected.item }}</b>
</div>
<div class="modal-footer">
<button class="btn btn-primary" ng-click="ok()">OK</button>
<button class="btn btn-warning" ng-click="cancel()">Cancel</button>
</div>
</script>
<button class="btn btn-default" ng-click="open()">Open me!</button>
<div ng-show="selected">Selection from a modal: {{ selected }}</div>
</div>
js:
var ModalDemoCtrl = function ($scope, $modal, $log) {
$scope.items = ['item1', 'item2', 'item3'];
$scope.open = function () {
var modalInstance = $modal.open({
templateUrl: 'myModalContent.html',
controller: ModalInstanceCtrl,
resolve: {
items: function () {
return $scope.items;
}
}
});
modalInstance.result.then(function (selectedItem) {
$scope.selected = selectedItem;
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
};
};
// Please note that $modalInstance represents a modal window (instance) dependency.
// It is not the same as the $modal service used above.
var ModalInstanceCtrl = function ($scope, $modalInstance, items) {
$scope.items = items;
$scope.selected = {
item: $scope.items[0]
};
$scope.ok = function () {
$modalInstance.close($scope.selected.item);
};
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
};
};
Live example: http://plnkr.co/edit/xIjUONEVhY5lO2kLhV4f?p=preview