I have a form setup in angular and am using ui-bootstrap for a modal dialog that appears before submission is complete. My site is setup in Sharepoint and uses a context to determine if they are on English or Spanish version of the site and display the proper language content. I want to pass the bool value IsInEnglish to the modal to display the correct <div> but I am not finding a solution to pass this value to the scope of the modal.
$scope.continue = function () { //this is off an ng-click on a button
var modalInstance = $modal.open({
templateUrl: '_layouts/Applications/VolunteerApp/modal/myModal.html',
controller: 'vAppController'
});
modalInstance.opened.then(function () {
$scope.modalIsInEnglish = IsInEnglish;
});
};
Add a resolve to $modal's instantiation object:
var modalInstance = $modal.open({
templateUrl: '_layouts/Applications/VolunteerApp/modal/myModal.html',
controller: 'vAppController',
resolve: {
myVar: function() {
return someVal;
}
}
});
and then pass someVal as an argument of the function that invokes the modal:
$scope.continue = function (someVal)
To access someVal in the modal controller, pass it in as an argument for the modal controller:
function myModalController($scope, someVal) { // do stuff }
Related
I am developing an Web Application using Angular JS. I am new to Angular JS. in my app I am using bootstrap.ui JS for Angular js. But I am having a problem with bootstrap modal controller. I open the bootstrap modal with new controller instance. Then I want to fire event back to parent controller when a button of modal is pressed.
I open Bootstrap modal when a button is clicked like below in a controller
var app = angular.module('memeApp',['ngRoute','ui.bootstrap','blockUI','ngFileUpload'],function($interpolateProvider) {
$interpolateProvider.startSymbol('<%');
$interpolateProvider.endSymbol('%>');
});
app.controller('DeleteConfirmModalController', ['$scope','$modalInstance','data',function ($scope, $modalInstance,data) {
$scope.closeDeleteConfirmModal = function () {
$modalInstance.dismiss('cancel');
};
$scope.deleteData = function()
{
//I want to call deleted function of DefaultController that opened current controller.
}
}]);
app.controller('DefaultController', ['$scope', 'Upload', '$timeout', '$http','$modal', function ($scope, Upload, $timeout , $http, $modal) {
$scope.deleted = function(param)
{
alert('deleted')
}
$scope.deleteTemplate = function(id,url)
{
var modalInstance = $modal.open({
controller: 'DeleteConfirmModalController',
templateUrl: $scope.deleteConfirmModalUrl,
resolve: {
data: function () {
return { id: id, url: url };
}
}
});
}
}]);
Let me explain my code above. When a user click a button in DefaultController, deleteTemplate function will be called. So that function open bootstrap modal creating new instance of DeleteConfirmModalController. When user click the delete button of bootstrap modal, deleteData function of modal controller will be called.
So I commented what I want to do inside that function. I want to call deleted function inside DefaultController. How can I call that function of parent controller from modal controller?
You can do so easily by passing the function you want to run as a callback to the modal.
vm.deleteData = function() {
// do something
}
Then pass to
var modalInstance = $uibModal.open({
animation: true,
templateUrl: 'vm.deleteConfirmModalUrl',
controller: 'DeleteConfirmModalController',
controllerAs: 'vm',
resolve: {
deletedCallback: function() {
return vm.deleted; // notice that I am passing a reference of `deleted` function
}
}
});
Then, inside the modal controller I wire an invocation to this callback function by a button click
.controller('ModalInstanceCtrl', function ($uibModalInstance, deletedCallback) {
// this will run on an ng-click
vm.runDeleted = function() {
if (angular.isFunction(deletedCallback)) {
deletedCallback("me");
}
}
...
}
I use angular.isFunction to test if the reference I passed to the modal controller is indeed of that of a function, and if it is, I run it, and I pass some value (in this case the string me) into the callback.
This code will run on the DeleteConfirmModalController controller.
Example plunk
I have a controller tied to my view via routing.
Example in App.js I have the following:
when('/about', { templateUrl: 'views/about.html', controller: 'appAboutCtrl' }).
I have another controller for Modal specific actions. I need this since it is a login action that applies across pages, and for that reason, it is it's own controller versus repeating the login functionality in every controller.
My modal does what it is supposed to do, but when I close it, I need it to update the $scope on the page to show/hide an item by setting something akin to:
$scope.isLoggedIn = true;
However, the scope is not updating. I have seen something like this here: https://github.com/angular-ui/bootstrap/issues/2110#issuecomment-54551321 but given the complexity of my login, I need it to be in its own controller... separate of the controller driven my view.
Here is the call to my Modal and controller specific modal in the controller tied to the view I open it from (about.html and appAboutCtrl):
// Open Modal for Login
$scope.login = function () {
var modalInstance = $modal.open({
templateUrl: 'templates/login.html',
controller: 'appLoginCtrl',
size: 'lg'
});
}
Here is where I try to set the scope on modal close in appLoginCtrl:
$scope.ok = function () {
$modalInstance.close();
$scope.isLoggedIn = true;
};
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
$scope.isLoggedIn = true;
};
I can reload the view on modal close, but can't set the $scope as follows:
$scope.ok = function () {
$modalInstance.close();
$route.reload();
};
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
$route.reload();
};
How can I set the $scope on Modal close so that when I close the modal, my view registers the change? My $scope is tied to a ng-show to show/hide an item on login.
You can use resolve option to bind variables.
var modalInstance = $modal.open({
templateUrl: 'templates/login.html',
controller: 'appLoginCtrl',
size: 'lg'
resolve: {
isLoggedIn: function () {
return $scope.isLoggedIn;
}
}
});
or you can pass the parent controller's scope to the modal scope
var modalInstance = $modal.open({
templateUrl: 'templates/login.html',
controller: 'appLoginCtrl',
size: 'lg',
scope: $scope
});
I'm using Angular UI Bootstrap. If a modal is open, I'd like to display true. If it's not open, I'd like to display false. Is there a way to do this within the HTML?
I tried using the following code, but it's wrong:
<code>myForm.$modalStack.opened={{myForm.$modalStack.opened}}</code>
Any thoughts on how to do this correctly?
Below the relevant code I'm using to trigger the modal:
HTML:
<button ng-click="myForm.agreement()">
Code in Controller:
.controller('MyFormCtrl',
function ($location, $stateParams, $modal, $scope, $http) {
var myForm = this;
// . . .
myForm.agreement = agreement;
function agreement() {
$modal.open({
templateUrl: 'views/agreement.html'
})
});
The opened property returned by $modal.open is a promise you can hook into.
So, using their example, see here - http://plnkr.co/edit/PsEqTIy8CDUU88HMLxbC?p=preview
$scope.modalOpen = false;
$scope.open = function (size) {
var modalInstance = $modal.open({
animation: $scope.animationsEnabled,
templateUrl: 'myModalContent.html',
controller: 'ModalInstanceCtrl',
size: size,
resolve: {
items: function () {
return $scope.items;
}
}
});
modalInstance.opened.then(function () {
$scope.modalOpen = true;
});
// we want to update state whether the modal closed or was dismissed,
// so use finally to handle both resolved and rejected promises.
modalInstance.result.finally(function (selectedItem) {
$scope.modalOpen = false;
});
};
You want to call the promises and then do whatever you need. .opened is a promise for when the modal opens, and .result is a promise for when the modal closes. So using this idea, you would use $scope.modalOpen as your boolean.
Whilst I have looked at the answers here, I'm still unable to trigger my openModal() function on page load.
Within my controller, I have the code (below question), which does allow openModal() to trigger fine either from an ng-click directive or when entering the following in the Chrome console:
angular.element($0).scope().openModal()
My problem is when I'm trying to conditionally open the modal upon page load. My current code is:
$scope.openModal = function (size) {
modalInstance = $modal.open({
templateUrl: 'myModalContent.html',
controller: 'pageBuilder',
size: 'lg',
resolve: {
items: function () {
return $scope.chosenDeploymentSchedule;
}
}
});
};
angular.element(document).ready(function () {
// $scope.openModal();
console.log('Hello World');
});
I do get my Hello World printing, but when I try uncommenting the $scope.openModal();, I get an infinite loop of undefined + Hello World printing out.
I don't know if this solution is too late for this question but may be useful for someone.
Create your normal bootstrap modal
$scope.myDialog = function () {
$uibModal.open({
templateUrl: 'templatefolder/modal.html',
backdrop: 'static',
windowClass: 'modal',
size: 'lg',
controller: function ($scope, $uibModalInstance) {
$scope.cancel = function () {
$uibModalInstance.dismiss();
};
}
});
};
Call the modal in your html page like so <span ng-init="myDialog()"></span>
That's it. This works for me and I hope it will work for someone too.
You can call $scope.openModal() directly underneath where you define it in the controller. It'll open the modal right away.
Whilst still unable to call it directly from within the same controller, my solution was to add another method which gets called from an ng-init in the view:
$scope.launchModalConditionally = function(){
if(!$scope.gotCredentials){
$scope.openModal();
}
};
I am trying to use Angular UI Bootstrap Modal. And I want to have the changes to the model to be shown immediately.
This is the controller:
$scope.testInputField = "";
$scope.showModal= function () {
var modalInstance = $modal.open({
templateUrl: 'partials/modal.html',
controller: 'ModalController',
resolve: {
inputTest: function () {
return $scope.testInputField;
}
}
});
modalInstance.result.then(function () {
// Do stuff
}, function () {
console.log("Some Error");
});
}
I have testInputField model also in my partial view.
And this is the the modal controller
controller('ModalController', [ '$scope', '$modalInstance', 'inputTest', function ($scope, $modalInstance, inputTest) {
$scope.inputTest = angular.copy(inputTest);
$scope.keyPressed = function (key) {
$scope.inputTest+= key;
console.log($scope.inputTest);
};
} ])
So basically what I want whenever changes are made to with the keyPressed method, the modal in my main controller to be changed at the same time. Is there a possibility to accomplish this with UI bootstrap modals current code
For this remove angular.copy method and try again. As it remove the data binding of the object.