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
Angular js - ngDialog how to open dialog from a controller?
提问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
}
}
});