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;
}
}
});
};
Related
need help while displaying json conect. When i press a username i need to display that particular user details in a modal or a table . any suggestions on how to do that using onclick ?
var mainApp= angular.module("mainApp", []);
mainApp.controller('TableFilterController', function($scope) {
$scope.clickMe = function(p) {
$scope.selected = p;
}
$scope.isSelected = function(p) {
return $scope.selected === p;
}
$scope.details = [
{
name : 'Mercury',
age : 0.4,
mass : 0.055,
descp : 'it is the hottest planet',
},
https://plnkr.co/edit/FluJQRlTkdsNfv1NLDhW?p=preview
You can popup a dilaog box using a $modal of bootstrap,
$modal.open({
templateUrl: 'myModalContent.html',
backdrop: true,
windowClass: 'modal',
controller: function($scope, $modalInstance, $log) {
$scope.selected = p;
$scope.submit = function() {
$log.log('Submiting user info.');
$log.log($scope.selected);
$modalInstance.dismiss('cancel');
}
$scope.cancel = function() {
$modalInstance.dismiss('cancel');
};
},
resolve: {
user: function() {
return $scope.selected;
}
}
});
DEMO WITH PLUNKER
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>
In my controller.js file I have:
angular.module('App').controller('AppCtrl', AppCtrl);
function AddApp(){
var vm = this;
vm.resetForm = resetForm;
function resetForm(){
vm.myForm.$setPristine();
}
}
...
function openModal(message){
var errorMessage = message;
var modalInstance = $modal.open({
templateUrl: 'url',
controller: 'AppCtrl',
controllerAs: 'App',
resolve: {
errorMessage: function () {
return errorMessage;
}
}
});
}
and in my modal I have
<div ng-controller="AppCtrl as App">
<form name=App.myForm>
...
when I use this format it tells me that
vm.myForm is undefined.
This is the closest thing I've found here, but it still did not work for me
Can I access a form in the controller?
You can pass the form by adding it to the resolve:
var modalInstance = $modal.open({
templateUrl: 'url',
controller: 'AppCtrl',
controllerAs: 'App',
resolve: {
errorMessage: function () {
return errorMessage;
},
myForm: function() {
return vm.myForm;
}
}
});
In your modal controller:
angular.module('app').controller('AppCtrl',
['$scope', 'errorMessage', 'myForm',
function ($scope, errorMessage, myForm) {
console.log(myForm);
}]
);
Id like to pass data from 1 view (master view ) to modal view using angular ui bootstrap plugin , below is my code which doesn't seem to work :
master view
vm.receipt_id = "1234"
<button type="button" class="btn btn-default" ng-click="vm.alertMeJimmy(vm.receipt_id)">Large modal</button>
controller
vm.alertMeJimmy = function(receipt_id) {
$uibModal.open({
animation: true ,
templateUrl: '/cashier/views/cashier.angular_components.modal',
controller: 'PatientsController',
controllerAs: 'vm',
resolve: {
receipt_id : function () {
return receipt_id ;
}
},
size: 'lg'
});
}
modal view
id like to access it like below in my modal view
<span ng-bind="vm.receipt_id " ></span>
Return an object instead of a primitive:
angular.module('App').controller('Controller', [
'$uibModal',
function ($uibModal) {
var vm = this;
vm.receipt_id = 1234;
vm.alertMeJimmy = function (receipt_id) {
var modalInstance = $uibModal.open({
size: 'sm',
templateUrl: 'modal.html',
controller: 'Modal as vm',
resolve: {
'receipt': { id: receipt_id }
}
});
}
}
]);
angular.module('App').controller('Modal', [
'receipt',
function (receipt) {
var vm = this;
vm.receipt = receipt;
}
]);
Example on Plunker: http://plnkr.co/edit/d9RLJLHr1zr9d5gVKHpD?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