5

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.

JPS
  • 2,730
  • 5
  • 32
  • 54

2 Answers2

1

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;
    }
Aco Mitevski
  • 353
  • 2
  • 9
  • sorry if I didn't explain the question properly. I don't want to use a separate controller for the ngDialog. There is an option to set like `scope : $scope`. This option will work only when the current controller is injected with `$scope`. but when the current controller follows vm syntax, then how this` scope` property of `ngDialog` should be? – JPS Sep 09 '15 at 13:52
  • yeah then I misunderstood your question. I don't how to pass the scope in this case. But as you simply want to access the student you could pass it as resolve param. See my modified version. – Aco Mitevski Sep 10 '15 at 07:42
  • You're saying to inherit parent scope on the ngDialog template you need to pass a resolve param with a function that returns the current scope var? One by one? Ew. – AlxVallejo Jan 03 '16 at 23:41
  • Also, doesn't this violate the Single Responsibility rule for Angular best practices? https://github.com/johnpapa/angular-styleguide#single-responsibility – AlxVallejo Jan 04 '16 at 16:12
0

I think you can omit it entirely. This seems to be working for me.

Tyson Williams
  • 1,630
  • 15
  • 35