Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set scope property of NgDialog when using controllerAs syntax

Hi I have StudentController as follows,

function StudentController($scope,StudentService){
    $scope.student = StudentService. getStudent();

   $scope.editStudent = function(){
    return ngDialog.openConfirm({
                      template: 'edit-student.html',
                      className: 'ngdialog-theme-default',
                      scope   : $scope // LINE 1
                });
   }
}

When editStudent function is invoked, I want to open a dialog to show edit option. And I want to use the $scope.student of StudentController itself in the edit-student.html as model data. for this feature, I can use scope property of NgDialog as scope:$scope (see LINE 1).

Now I am trying to change the StudentController as suggested in the Angular-StyleGuide where I am not going to use the $scope in controller at all. In that case, how can I access student in edit-student.html?

function StudentController(StudentService){
        var vm = this;
        vm .student = StudentService.getStudent();

        return ngDialog.openConfirm({
                          template: 'edit-student.html',
                          className: 'ngdialog-theme-default',
                          scope   : ???
                         // $scope is not used in this controller. 
                         //Then what should I send instead?
                         // I tried using scope :  vm . But it didn't work. 
                    });
        }

Update : updated with more details to avoid confusion.

like image 525
JPS Avatar asked Nov 10 '22 05:11

JPS


1 Answers

I think you're confusing it a bit. If you want to use controllerAs Syntax you need an own controller for the dialog. e.g.

function StudentController(StudentService){
    var student = StudentService.getOne();
    return ngDialog.openConfirm({
                      template: template,
                      className: 'ngdialog-theme-default',
                      controller: DialogController
                      controllerAs: 'vm',
                      resolve: {student: function() {return student; } }
                });
    }

    function DialogController(student) {
                var vm = this;
                vm.student = student;
    }
like image 56
Aco Mitevski Avatar answered Nov 15 '22 05:11

Aco Mitevski