Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$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
            });
        };

    }
like image 254
tester123 Avatar asked May 12 '16 13:05

tester123


1 Answers

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.

like image 77
optimus Avatar answered Oct 21 '22 02:10

optimus