Using a promise (standard way)
The standard way to return data back to the calling controller after closing the modality is to register a callback after the promise has been removed:
modalInstance.result.then(function (selectedItem) { $scope.selected = selectedItem; }, function () { $log.info('Modal dismissed at: ' + new Date()); });
Using Events
You can also send an event up the hierarchy of regions by calling $ emit :
$scope.$emit(name, args);
Then the parent area can listen for the event by calling $ on :
$scope.$on('eventName', function(event, data) { console.log(data); });
When creating a new modal value, $ scope will be a child of the size of $ rootScope by default. When $ emit () is called, it scans its own region (i.e., the $ modal region) for any registered listeners, calls them (if present), and then crosses the chain of regions up until the parent ends. If the parent scope is not specified when creating the modal object, call $ scope. $ Emit ('eventName', data) will bypass the controller scope when evaluating the scope chain. Both the calling controller and the modal controller can work directly with $ rootScope when calling $ emit () and $ on (), but you usually want to associate the modal region with it, invoking the hierarchy of the $ -region of the controller. To set the parent modal scope, set the scope parameter to the $ scope scope as follows:
var modalInstance = $modal.open({ templateUrl: 'myModalContent.html', backdrop: true, windowClass: 'modal', controller: ModalInstanceCtrl, scope: $scope, resolve: { mkdir: function () { return $scope.mkdir; } } });
Derek greer
source share