Skip to content
This repository has been archived by the owner on May 29, 2019. It is now read-only.

dialog does not inherit scope of controller #259

Closed
gnesher opened this issue Mar 23, 2013 · 8 comments
Closed

dialog does not inherit scope of controller #259

gnesher opened this issue Mar 23, 2013 · 8 comments

Comments

@gnesher
Copy link
Contributor

gnesher commented Mar 23, 2013

It seems the dialog $scope does not inherit from the controller which created it.

I'm new to Angularjs, but I'm guessing the fact that the dialog is injected into the html body (and not the containing controller) might be causing this?

edit
I know I can use resolve, but I've got multiple objects that needs to be passed which isn't very comfortable (and slightly counter intuitive assuming I'm creating a scope anyway)

@petebacondarwin
Copy link
Member

This is by design. The $dialog service does not have any knowledge of what
is calling it and so it doesn't know anything about the scope of the
caller. It creates a new scope off the $rootScope.
If you want to pass in information from the "current" scope, then you can
use the resolves parameters.

On 23 March 2013 20:47, gnesher [email protected] wrote:

It seems the dialog $scope does not inherit from the controller which
created it.

I'm new to Angularjs, but I'm guessing the fact that the dialog is
injected into the html body (and not the containing controller) might be
causing this?


Reply to this email directly or view it on GitHubhttps://github.com//issues/259
.

@pkozlowski-opensource
Copy link
Member

@gnesher as @petebacondarwin pointed out this is by design really. What we are trying to do is to treat dialogs the same way as routes in AngularJS.

I know that the current API is not ideal and it will probably will go through some changes, so if you've got any suggestions to make it better we are all ears. But the thing is that it should be possible to open a dialog from any place, even from a service where there is no "current scope" and the only thing you can work with is the $rootScope.

I acknowledge that the syntax used by the resolve is not ideal but if you've got many objects you can group them together, sth like:

resolve: {
   dialogsModel: function() {
     return //return complex model here;
   }
}

Going to close this one for now, let's move eventual discussion about API changes to:
#170

@gnesher
Copy link
Contributor Author

gnesher commented Mar 23, 2013

@pkozlowski-opensource I'm sadly not familiar enough with Angularjs so what I'm suggesting might not make sense, but couldn't we just pass an parent scope parameter? if it gets passed the scope is instantiated as a child, otherwise it just get assigned to $rootScope?

@petebacondarwin
Copy link
Member

I think you need to rethink what you are doing. If you want to pass
variables to the scope of the dialog box then use the resolves. This will
benefit your development in the long run as you will be being more explicit
about what is passed through to the dialog rather than letting the whole
scope through. This is analogous to an isolated scope widget directive.

On 23 March 2013 22:08, gnesher [email protected] wrote:

@pkozlowski-opensource https://github.com/pkozlowski-opensource I'm
sadly not familiar enough with Angularjs so what I'm suggesting might not
make sense, but couldn't we just pass an parent scope parameter? if it gets
passed the scope is instantiated as a child, otherwise it just get assigned
to $rootScope?


Reply to this email directly or view it on GitHubhttps://github.com//issues/259#issuecomment-15346812
.

@mlarcher
Copy link

For those wondering how to make use of the resolve (as I was), the properties passed in the resolve object through the dialog options are available as a dependency in the controller.
In the exemple suggested by pkozlowski, passing

var dialog = $dialog.dialog(
    controller: 'MyPopinCtrl',
    resolve: {
       dialogsModel: function() {
         return //return complex model here;
       }
});

in the options will let you access 'dialogsModel' as a dependency in MyPopinCtrl with something like this :

app.controller('MyPopinCtrl',
['$scope', 'dialog', 'dialogsModel',
function ($scope, dialog, dialogsModel) {
    var key;

    // hook the passed data to the popin scope
    for (key in dialogsModel) {
        $scope[key] = dialogsModel[key];
    }

    $scope.close = function (e, result) {
        e.preventDefault();
        dialog.close(result);
    };

}]);

I hope this can help.

@multimike
Copy link

I find the reasoning that a dialog shall have it's own isolated scope a bit strange.
I've noticed many dialogs are designed to be used in a specific use case, and is therefore only opened from a particular location in the application. It would be reasonable to access parent scope in those cases.
(Resolves is working fine as a substitute in my case, but still.)

@runxc1
Copy link

runxc1 commented Jun 26, 2013

So like everyone else here I thought for sure that the dialog's scope inherited from the one that created it... That being said I don't need to pass data to the dialogs scope. Rather I need to call a method on the creating scope so that the dialog can update elements on the main scope and have the UI reflect this. How do I accomplish this?

@pkozlowski-opensource
Copy link
Member

@runxc1 when #441 is done you will be able to pass a scope to the $dialog service.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants