9

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';
        }
    }
});
Jahongir Rahmonov
  • 13,083
  • 10
  • 47
  • 91

1 Answers1

13

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

dfsq
  • 191,768
  • 25
  • 236
  • 258
  • help needed!!! As you said, it sends the variable to the controller and in the controller I assign it to $scope.variable. But that controller has stopped being responsible for the template :( $scope.variable cannot be seen in the template(like: {{name}}) – Jahongir Rahmonov Jan 13 '15 at 11:58
  • Check updated demo, you also need to pass `scope: $scope` for this approach. If you don't specify `scope` option `ngDialog` creates child scope of the `$rootScope` and compile template in this scope. However in `someCtrl` you are dealing with child scope of the `$scope` passed in during `$controller` instantiation. – dfsq Jan 13 '15 at 12:00
  • your demo is perfectly working as I want. But it the same thing is not working for me. Maybe because of the angular or ngDialog versions – Jahongir Rahmonov Jan 13 '15 at 12:02
  • Make sure you open dialog with `scope: $scope,` options and use the same `$scope` for `$controller('someCtrl', {$scope: $scope})`. – dfsq Jan 13 '15 at 12:06
  • Ok, now it is working! I misunderstood what you said by scope: $scope, as there were two such lines. Now it is fixed! THank you :) – Jahongir Rahmonov Jan 13 '15 at 12:07