Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

send variables to controller('resolve') in ngDialog

Simple way of opening modal with ngDialog is this:

ngDialog.open({
    template: 'template.html',
    controller: 'someCtrl'
})

How can I send variables to that 'someCtrl'?

Is there such thing as 'resolve' in ngDialog?

Example from angular-bootstrap modal:

$modal.open({
    template: "<p>This is template</p>",
    controller: "someCtrl",
    resolve: {
        someVar: function(){
            return "Value of someVar"
        }
    }
})

this would open the modal send the 'someVar' to the responsible Controller.

UPDATE:

It seems like new version of ngDialog added this feature:

ngDialog.open({
    controller: function Ctrl(dep) {/*...*/},
    resolve: {
        dep: function depFactory() {
            return 'dep value';
        }
    }
});
like image 367
Jahongir Rahmonov Avatar asked Jan 13 '15 11:01

Jahongir Rahmonov


1 Answers

Well looks like ngDialog doesn't support resolve and custom injection in controller. However you can alway do it manually by creating controller instance yourself:

ngDialog.open({
    scope: $scope,
    template: 'template.html',
    controller: $controller('someCtrl', {
        $scope: $scope,
        name: 'Thomas'
    })
});

then in controller you will be able to access injected service/variable:

app.controller('someCtrl', function($scope, name) {
    console.log(name); // Thomas
});

However there is a caveat with this approach, because when controller in instantiated by ngDialog itself it also injects $element service in it, which is an angular.element instance of the opened dialog HTML (however I doubt it's even necessary in controller). But you should know it anyway.

Demo: http://plnkr.co/edit/3YpQ2bemk8fntKAPWY9i?p=preview

like image 191
dfsq Avatar answered Oct 12 '22 11:10

dfsq