angular-strap icon indicating copy to clipboard operation
angular-strap copied to clipboard

how to pass model's data to modal

Open ghost opened this issue 13 years ago • 15 comments

How can I pass data from a specific model to modal ? For example: I have a list of product, I click on product's image, it should pass that product information to the opened modal

ghost avatar Jan 07 '13 21:01 ghost

The modal will inherit the scope of the opener button. So any variable set on this scope will be available inside the modal. That is what happens on the docs.

You can also define a specific controller with a custom scope for your modal (using the ng-controller attr on the opener button).

You can use this plnkr to test : http://plnkr.co/edit/xKP5PW3Dco1CczmCuuVN?p=preview

mgcrea avatar Jan 08 '13 01:01 mgcrea

Thanks for your quick answer :)

ghost avatar Jan 08 '13 05:01 ghost

how could the same thing happen using the $modal service? Since that's only creating a single modal instead of multiple modals. Wouldn't that modal need to know which item in the list it is representing? The reason being that the controller that handles the internal function of the modal is making a number of service calls when it is created. If I have a bunch of things in a list and i just opt to use the the simple modal, wouldn't it make the service calls for each one?

a2thek26 avatar Jul 19 '13 17:07 a2thek26

I guess to clarify...

In my parent controller...

$scope.items [{...},{...}, etc...];

$scope.showModal = function() {
  var modalPromise = $modal({
      template: 'modal.html',
      show: false,
      persist: true,
      backdrop: 'static',
      modalClass: 'modal-class',
      scope: $scope
    });

    $q.when(modalPromise).then(function(modalEl) {
      modalEl.modal('show');
    });
}

In my view...

<ul class="item-list">
  <li ng-repeat="item in items">
    {{item.id}} - {{ item.name }} <a ng-click="showModal()">Edit</a>
  </li>
</ul>

In my template...

<div class="modal-body">
  <h1>{{ item.name }}</h1>
  <p>{{ item.description }}</p>
 etc...
</div>

I'm passing the $scope in but it's just the whole scope on the parent controller so the modal template doesn't know what "item" is. It knows what "items" is since its part of the scope.

The only thing I can think of is to just create $scope.item = {} and change showModal() to showModal($index), then in $scope.showModal, add $scope.item = $scope.items[id].

Not sure if this is hacky or I'm just missing something simple.

a2thek26 avatar Jul 19 '13 19:07 a2thek26

We also have this issue

I was wondering how you can access the scope of the modal in order to call a method to initialize the modal at each 'show'.

When I try to get the scope of modalEl.scope(), it looks like it returns the $rootScope(since the modal is appended to the body?).

Any suggestion to be able to access the scope of the modal controller in order to call a function and/or set variables?

Thanks.

benorama avatar Jul 24 '13 07:07 benorama

@a2thek26 I think passing along the $index would be a good solution.

After that you could do:

var modalPromise = $modal({
      template: 'modal.html',
      show: false,
      persist: false, // destroy the new scope on hide
      backdrop: 'static',
      modalClass: 'modal-class',
      scope: $scope.$new({item: $scope.items[index]}) // create a new scope to mimic the ngRepeat scope.
    });

mgcrea avatar Jul 24 '13 19:07 mgcrea

@benorama indeed it returns the $rootScope might be a bug.

If you need tight control over the modal scope, you might want to set it up before and retain a reference in a variable:

var scope = $scope.$new({foo: 'bar'});
    var modal = $modal({
      template: 'modal.html',
      show: true,
      backdrop: 'static',
      scope: scope
    });

mgcrea avatar Jul 24 '13 19:07 mgcrea

Thanks Olivier!

Indeed, creating a new scope solve the issue. I was able to access the first child element where the controller is defined to access its scope and an init method.

var scope = $scope.$new({foo: 'bar'});
var modal = $modal({
  template: 'modal.html',
  show: true,
  scope: scope
 });

$q.when(modalPromise).then(function(modalEl) {
  modalEl.modal('show');
  modalEl.children(':first').scope().init({postId: postId});
});

Strangely, if I use the current scope instead of a new one, it does not work.

benorama avatar Jul 24 '13 20:07 benorama

There is still an issue accessing variables in the scope when a modal is created using the $modal service.

This is due to line 38 on modals.js: var scope = $modal.$scope = options.scope && options.scope.$new() || $rootScope.$new();

A new scope is created regardless of the scope passed in. Variables assigned to the scope in the controller will be visible using the $parent variable.

It will be nice if there was a way to specify not to change the scope.

mb-dev avatar Jul 19 '14 03:07 mb-dev

I found a workaround:

After activating the modal in a controller using:

 modal = $modal({...})

it's possible to access the $scope using modal.$scope and assign the values there.

modal.$scope.item = {value: 1}
modal.$scope.onSave = function() {
  modal.hide()
}

mb-dev avatar Jul 19 '14 04:07 mb-dev

Nice mb-dev, worked as expected.

wiliame avatar Jul 23 '14 22:07 wiliame

modal.$scope.item = {value: 1}
modal.$scope.onSave = function() {
  modal.hide()
};

Been looking all over for this documentation. Wish it was within the angular-strap docs! Thank you!

sparkyfen avatar Jan 12 '15 23:01 sparkyfen

A new scope is created regardless of the scope passed in. Variables assigned to the scope in the controller will be visible using the $parent variable.

Modal.js var scope = $modal.$scope = options.scope && options.scope.$new() || $rootScope.$new();

Is it possible to change this so it does not create a new scope?

timelf123 avatar Jul 16 '15 17:07 timelf123

@mb-dev If you don't use primitives (eg. if you use dots in your variables expressions), you won't need to traverse the scope hierarchy with $parent. Using primitives values directly on your scope is considered bad practice.

@timelf123 Regarding not creating a scope, it would leak the inner implementation to your parent scope (publish some private props, etc.), that could somehow conflict with your code, or another directive. Also creating scope is a safe way to manage memory as we can correctly cleanup everything when the modal get's destroyed.

mgcrea avatar Jul 18 '15 10:07 mgcrea

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

lock[bot] avatar Jan 26 '19 03:01 lock[bot]