Garuuk
Garuuk

Reputation: 2244

angular accessing modal parent controller

I'm using ui-router with $modal and my set up is like this in my routing page:

.state('resourcesControl.resource.dataStuff', {
        url: "/:resourceId/dataStuff",
        onEnter: ['$stateParams', '$state', '$modal', '$timeout', 'resourceService', function($stateParams, $state, $modal, $timeout, resourceService){

            var modalInst = $modal.open({
                templateUrl: "templates/dataStuff.html",
                windowClass: 'data-modal',
                controller: 'dataStuffCtrl'
            });

            modalInst.result.then(function(){

            }, function(){
                $state.go('resourcesControl.resource');
            });
        }]
    })

My understanding with UI router is that all the child states have access to their parents controller and scope variables. In theory my dataStuffCtrl should have access to resource and resourcesControl controllers and their scopes.

However, when I wrap curly brackets around a parent scope item in the dataStuff view, nothing renders. Is there a way around this? I remember seeing other people posting about parent controllers with $modal but I can't find them on SO atm.

Upvotes: 1

Views: 1910

Answers (1)

David Beech
David Beech

Reputation: 1108

The issue is that the $modal service adds the element to the end of the document. So it doesn't sit in you scope hierarchy and instead is only a child of the $rootScope. You have a couple of options.

  1. Pass the data to the modals controller via the $rootScope or broadcast/emit events (messy)
  2. Manually pass the modal the scope you want to use as part of the modal.open options object via the scope property. (You can call $scope.$new() to manually create a child scope).
  3. Use the resolve property of the modal.open options object to pass the data to be injected into the modals controller just like angular route resolves

Upvotes: 1

Related Questions