javascript Angular js - ngDialog 如何从控制器打开对话框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21427796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 20:54:06  来源:igfitidea点击:

Angular js - ngDialog how to open dialog from a controller?

javascriptangularjsmodal-dialogng-dialog

提问by itsme

Hi i installed the following module https://github.com/likeastore/ngDialog

嗨,我安装了以下模块https://github.com/likeastore/ngDialog

it works great but i can't get how to open the dialog box from a controller, i do:

它工作得很好,但我无法从控制器中打开对话框,我这样做:

var app = angular.module('app', ['ngRoute','ngAnimate','ngSanitize','ngDialog']);

app.controller('SignupController', function($rootScope,$scope) {
    $scope.signup = function(){
        var error = false,
        error_list = "ERRORS";

        if(!$scope.signup_username){
            error = true;
            error_list += "\n\n Username \n " + $rootScope.errors.required_field;
        }if(!$scope.signup_email){
            error = true;
            error_list += "\n\n Email \n " + $rootScope.errors.valid_email;
        }if(!$scope.signup_password){
            error = true;
            error_list += "\n\n Password \n " + $rootScope.errors.required_field;
        }

        if(error){
            ngDialog.open({template:error_list,plain:true});
        }else{
            //register
        }
    }

});

But it doesn't works as expected, cause i get console error : ngDialog is not defined.

但它没有按预期工作,因为我收到控制台错误:未定义 ngDialog

回答by Maurice

Your controller is not taking a dependency on ngDialog so it is an unknown variable at runtime.

您的控制器不依赖于 ngDialog,因此它在运行时是一个未知变量。

app.controller('SignupController', function($rootScope, $scope, ngDialog) {
    $scope.signup = function(){
        var error = false,
        error_list = "ERRORS";

        if(!$scope.signup_username){
            error = true;
            error_list += "\n\n Username \n " + $rootScope.errors.required_field;
        }if(!$scope.signup_email){
            error = true;
            error_list += "\n\n Email \n " + $rootScope.errors.valid_email;
        }if(!$scope.signup_password){
            error = true;
            error_list += "\n\n Password \n " + $rootScope.errors.required_field;
        }

        if(error){
            ngDialog.open({template:error_list,plain:true});
        }else{
            //register
        }
    }
});