tester123
tester123

Reputation: 1113

$mdDialog and controllers

I have two $mdDialog that I want to used in two different views, the controller is the exact same for each. What I want is to have the $mdDialog.show() function in each controller, passing an object and the one controller file to do the work.

But when I try this I get an error saying DialogCtrl is not defined.

For Example, In the view controller:

angular.module('myApp')
        .controller('myViewController', [
            '$scope',
            '$mdDialog',
            myViewController
        ]);

function myViewController($scope, $mdDialog, myObj) {

    var obj = myObj;
    vm.showDialog = function(event) {
            $mdDialog.show({
                    controller: DialogCtrl,
                    templateUrl: 'partials/dialog.html',
                    clickOutsideToClose: false,
                    parent: angular.element(document.body),
                    targetEvent: event,
                    openFrom: {left: 1500},
                    closeTo: {left: 1500},
                    locals: { object: obj }
                })
                .then(function(response) {
                 //do stuff
                });

};

Then my dialog controller:

angular.module('myApp')
        .controller('DialogCtrl', [
            '$scope',
            '$mdDialog',
            'object',
            DialogCtrl
        ]);

    function DialogCtrl( $scope, $mdDialog, object ) {
        $scope.obj = object;

        $scope.answer = function(answer) {
            $mdDialog.hide({
                answer: answer,
                dataToPass: $scope.obj.name
            });
        };

    }

Upvotes: 5

Views: 7009

Answers (1)

optimus
optimus

Reputation: 856

Try putting controller name under quotes:

$mdDialog.show({
                controller: 'DialogCtrl',
                templateUrl: 'partials/dialog.html',
                clickOutsideToClose: false,
                parent: angular.element(document.body),
                targetEvent: event,
                openFrom: {left: 1500},
                closeTo: {left: 1500},
                locals: { object: obj }
            })

This will remove the error.

Upvotes: 13

Related Questions