加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 使用controllerAs语法时如何设置NgDialog的scope属

发布时间:2020-12-17 18:08:43 所属栏目:安全 来源:网络整理
导读:嗨我有StudentController如下, function StudentController($scope,StudentService){ $scope.student = StudentService. getStudent(); $scope.editStudent = function(){ return ngDialog.openConfirm({ template: 'edit-student.html',className: 'ngdialo
嗨我有StudentController如下,

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

当调用editStudent函数时,我想打开一个对话框来显示编辑选项.我想在edit-student.html中使用$scope.student的StudentController本身作为模型数据.对于这个特性,我可以使用NgDialog的scope属性作为范围:$scope(参见LINE 1).

现在我正在尝试按照Angular-StyleGuide中的建议更改StudentController,我根本不会在控制器中使用$scope.在这种情况下,我如何在edit-student.html中访问学生?

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

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

更新:更新了更多细节以避免混淆.

解决方法

我觉得你有点困惑.如果要使用controllerAs语法,则需要一个自己的控制器来进行对话.
例如

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

    function DialogController(student) {
                var vm = this;
                vm.student = student;
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读