how to pass model's data to modal
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
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
Thanks for your quick answer :)
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?
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.
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.
@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.
});
@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
});
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.
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.
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()
}
Nice mb-dev, worked as expected.
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!
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?
@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.
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.