How to pass data into an angular.js UI bootstrap modal controller - angularjs

Using Angular.js UI bootstrap modal, how can I pass data into a modal popup's controller? I am currently trying:
var modalInstance = $modal.open({
templateUrl: 'partials/confirmation-modal.html',
controller: 'confirmationModal',
resolve: {
foo: function() { return 'bar'; }
},
backdrop: 'static',
keyboard: true
});
The controller confirmationModal is:
(function(_name) {
/**
* #ngInject
*/
function _controller($scope, foo) {
console.log(foo);
}
angular
.module('myApp')
.controller(_name, _controller);
})('confirmationModal');
But this errors with:
Unknown provider: fooProvider

You can try to define the "confirmationModal" controller with angular.module('app').controller(...) instead.
If you want to use a string to refer to a controller, you need to register it to an angular module.
So, you have two ways to make it work:
1.Use string
In modal settings:
controller: "confirmationModal"
In controller definition (assume "app" is your module name):
angular.module('app').controller("confirmationModal", function($scope, foo) {
console.log(foo);
});
2.Use function itself
In modal settings:
controller: confirmationModal
In controller definition:
var confirmationModal = function($scope, foo) {
console.log(foo);
}
Can you tell the difference?

Related

Using angular 1.5 angular.js:Unknown provider: $uibModalInstanceProvider

I have an Angular 1.2 project, and I have a controller which I want to replace using Component. This new component is open from uibModal and it contains another directive.
Before changing it to a component, everthing was fine, but after I changed it I get an error: Unknown provider: $uibModalInstanceProvider
First component:
$ctrl.openImportModal = function () {
var modalInstance = $uibModal.open({
templateUrl: 'forecastDataNew/modals/importStaffingPlanJobModal/importStaffingPlanJobModal.tpl.html',
component: 'importStaffingPlanJobModalCtrl',
windowTopClass: 'import-forecast-data-modal',
size: 'sm',
backdrop: 'static'
});
...
Second component (which throws the error when I change a component to a controller):
angular.module('nice.saas.wfm.importStaffingJob')
.component('importStaffingPlanJobModalCtrl', {
templateUrl: 'forecastDataNew/importStaffingPlanJobs.tpl.html',
bindings: { },
controller: function($q, $scope, $log, $uibModalInstance, Utils, ForecastDataService) {
'use strict';
$scope.filePicked = false;
$scope.file = { };
$scope.isClicked = true;
$scope.uploadCsvAndSave = function(file) {
This component includes its HTML directive — if I remove that directive, no error occurs.
angular.module('nice.saas.wfm.importStaffingJob',['firebase', 'ui.bootstrap'])
Try with this module definition.

moving angular modal controllers into separate files

I have a page controller on my site that includes several modals. Some of the modal controllers are getting robust, and I would like to move them into separate controllers without losing the scope of the outer page controller (the modal controller uses some of the page controllers functions and properties) - is this possible? So far I am getting errors to anything which references the outer controller. Here is a simple example of how I have it set up:
the page controller:
angular.module('myApp')
.controller('outerCtrl', function(MetaService) {
var thisCtrl = this;
thisCtrl.someFunction = function() {
//some cool functionality that will elvaluate something
}
function optionsModal() {
var PageCtrl = thisCtrl;
$uibModal.open({
'controller': 'scripts/controllers/optionsModal.js',
'controllerAs': 'ModalCtrl',
'templateUrl': 'views/modals/optionsModal.html',
'size': 'md'
});
}
});
the modal controller:
angular.module('myApp')
.controller('optionsModalCtrl', function(MetaService) {
var modalCtrl = this;
function giveOptions() {
if (PageCtrl.someFunction()) {
//offer some option
} else {
//offer a different option
}
}
});
Here is the example from official page:
var modalInstance = $uibModal.open({
animation: $scope.animationsEnabled,
templateUrl: 'myModalContent.html',
controller: 'ModalInstanceCtrl',
size: size,
scope: $scope, // pass parent scope to modal instance
resolve: {
items: function () {
return $scope.items;
}
}
});

Angular ui-router: $stateParams empty inside my directive

In my angular app, I have created a custom directive for a navbar, which controller takes in $stateParams to access a variable called lang, as so:
.config(function($stateProvider, $urlRouterProvider, LANG) {
$urlRouterProvider
.otherwise('/' + LANG['EN'].shortName);
$stateProvider
.state('proverb-list', {
url: '/:lang',
templateUrl: 'components/proverb-list/proverb-list.html',
controller: 'ProverbListCtrl as vm'
})
.state('proverb-single', {
url: '/:lang/:proverbId',
templateUrl: 'components/proverb-single/proverb-single.html',
controller: 'ProverbCtrl as vm'
});
});
When I access the state proverb-list, the controller named ProverbListCtrl does see $stateParams.lang correctly, but my navbar directive cannot. When I console.log($stateParams) all I get is an empty object.
This navbar is outside my ui-view:
<navbar proverbial-navbar></navbar>
<div ui-view></div>
<footer proverbial-footer></footer>
Is that the problem? How can I access the actual $stateParams inside my directive?
EDIT: directive code below, as asked:
(function() {
'use strict';
angular
.module('proverbial')
.directive('proverbialNavbar', directive);
function directive() {
var directive = {
restrict: 'EA',
templateUrl: 'components/shared/navbar/navbar.html',
scope: {
},
link: linkFunc,
controller: Controller,
controllerAs: 'vm',
bindToController: true
};
return directive;
function linkFunc(scope, el, attr, ctrl) {
}
}
Controller.$inject = ['LANG', 'ProverbFactory', '$stateParams'];
function Controller(LANG, ProverbFactory, $stateParams) {
var vm = this;
vm.languages = LANG;
console.log($stateParams);
vm.currentLang = LANG[$stateParams.lang.toUpperCase()];
activate();
function activate() {
vm.alphabet = ProverbFactory.getAlphabet();
}
}
})();
You should not access $stateParams in this directive when it is independent of the state. Since your navbar is outside of the ui-view, its controller can be called before the ui-router has initialized the $stateParams of the state you are interested in. Remember that your navbar controller is called only once, when the navbar is initialized and not every time the state changes.
Alternative: What you can do is turn your currentLang field into a function. The function can retrieve the value from the $stateParams when needed:
vm.currentLang = function() { return LANG[$stateParams.lang.toUpperCase()] };
Make sure you change currentLang to currentLang() everywhere in your template.

Creating Modal Popup using AngularJS and Bootstrap

Hi I am trying to create a simple modal dialog that pops up when a user clicks a button. I am brand new to Angular and Bootstrap and I'm having a hard time figuring it out. I've created a plnkr here
(function () {
'use strict';
angular
.module('crm.ma', ['ui.bootstrap'])
.controller('AdvancedSearchCtrl', function ($modal) {
vm.openModal = function () {
var modalInstance = $modal.open({
templateUrl: 'topnav_advancedmodal.html',
controller: 'searchCtrl as modal'
});
}
})
});
http://plnkr.co/edit/VgQqRIMGewuwQPnUxm87?p=catalogue
plnkr code above. Please help!
You have several issues with your code. Here are some of them:
JavaScript
(function() {
"use strict";
angular.module('crm.ma', ['ui.bootstrap']). // You define new module with angular.module('...', []) syntax. If module is already initialised, use angular module('...') instead
controller('searchCtrl', function() {}). // Make sure this controller exists and registered in angular
controller('advancedSearchCtrl', ['$modal',function ($modal) { // Use ['x', function(x) {...}] syntax
this.openModal = function () { // vm -> this
var modalInstance = $modal.open({
templateUrl: 'topnav_advancedmodal.html',
controller: 'searchCtrl as modal' // <- make sure searchCtrl controller exists and is registered
});
};
}]);
}());
Plunker
http://plnkr.co/edit/6X40tgEriHXTjBsfvkHy?p=preview

Angular how to get access to $scope var from modal

I have a page where I have a button to launch a modal. Both pages has its own controllers. The question is how to get variable from page in modal controller?
You pass data to your modal controller using resolve.
var modalInstance = $modal.open({
templateUrl: 'template.html',
controller: 'MyModalCtrl',
resolve: {
variableToPass: function () {
return $scope.items;
}
}
});
Then you define your modal controller like this
myApp.controller('MyModalCtrl', ['$scope', $modalInstance'', 'variableToPass', function($scope, $modalInstance, variableToPass) {
...
}]);
Alternatively, or complementary, you can pass the whole $scope like this
var modalInstance = $modal.open({
templateUrl: 'template.html',
controller: 'MyModalCtrl',
scope: $scope
});

Resources