How do I pass an OPTIONAL parameter to my angularJS modal? Here is my code:
CONTROLLER A (TRIGGER):
$modal.open({
templateUrl: 'UploadPartial.html',
controller: 'photos.uploadCtrl',
resolve: {
preselectedAlbum: function preselectedAlbum() {
return angular.copy($scope.selectedAlbum);
}
}
});
CONTROLLER B (MODAL):
app.controller('photos.uploadCtrl', [
'$scope',
'$modalInstance',
'$injector',
function uploadCtrl($scope, $modalInstance, $injector) {
if ($injector.has('preselectedAlbum')) {
console.log('happy'); // I want this to work, but $injector doesn't find it
} else {
console.log('sad'); // Always gets here instead :(
}
}
]);
NOTE: It works when i put preselectedAlbum
as a dependency, but then i get the error whenever I don't explicitly pass it in. I want it to be optional instead.
Attach a value to the modal controller
angular.module('app')
.controller('TestCtrl',TestCtrl)
.value('noteId', null); // optional param
Other than the resolve:
, you could also pass values to the modal controller via scope:
.
$scope.preselectedAlbum = angular.copy($scope.selectedAlbum);
$modal.open({
templateUrl: 'UploadPartial.html',
controller: 'photos.uploadCtrl',
scope: $scope,
});
and then in the modal controller:
function uploadCtrl($scope, $modalInstance) {
if ($scope.preselectedAlbum) {
console.log('happy');
} else {
console.log('sad');
}
}
Example plunker: http://plnkr.co/edit/ewbZa3I6xcrRWncPvDIi?p=preview
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With