Question

I am using angular-bootstrap $modal and I have issue understand how to share data between a controller and the modal.

Controller:

app.controller 'MainController', ($scope, $templateCache) ->      
  $scope.openModal = ->
    modal = $modal.open(
      template: $templateCache.get('modal.html')
      controller: modalController
      size: 'sm'
      resolve:
        repository: ->
          $scope.repository
    )

Modal:

modalController = ($scope, $modalInstance, repository) ->
  $scope.repository = repository

  $scope.update = (other_repo) ->
    $scope.repository = other_repo  
    $modalInstance.dismiss('cancel')

At this point I expect $scope.repository from MainController to be updated when it is changes from the modal, but it is not the case.

What am I missing?

Thank you

Was it helpful?

Solution

The modal controller has a scope that is different from the controller which opened the modal. To get back some data from the dialog once it's closed, you should get it from the result promise, as shown in the example in the documentation:

modal = $modal.open(...);

modal.result.then(function (otherRepo) {
  $scope.repository = otherRepo;
}

OTHER TIPS

If you not passing controller's scope into $modal, it create new one, prototypically inherited from the $rootScope: var modalScope = (modalOptions.scope || $rootScope).$new(); and there is no connection to opener's controller scope from this one. But you can pass into modal scope of controller which open it:

  $scope.open = function (size) {
    var modalInstance = $modal.open({
      templateUrl: 'myModalContent.html',
      controller: ModalInstanceCtrl,
      size: size,
      scope: $scope
    });

This time, scope in ModalInstanceCtrl will inherit parent controller's scope and all changes will be reflected in opener controller's scope.

Look at this plunker (it's slightly changed sample from Bootstrap-UI) selected object from opener scope updated in modal and this update immediately reflected in view of the ModalDemoCtrl controller.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top