Node.js使用对话框ngDialog的示例代码

做网站经常会遇到弹出对话框获取用户输入或弹出对话框让用户确认某个操作之类的情景,有一个基于AngularJS的扩展模块可以帮我们优雅地完成这类事情:ngDialog

ngDialog在github上提供了一个示例网页,演示了它的各种用法,在这里:https://github.com/likeastore/ngDialog/blob/master/example/index.html。ngDialog的github主页的readme也对常用的指令和服务做了较为详细的介绍,可以参考。我这篇就纯粹是参考ngDialog的示例来的。

创建对话框可以是用ngDialog.open(options)或ngDialog.openConfirm(options)。前者打开一个普通的对话框,可以通过options制定诸如主题、模板等一系列属性,后者打开一个默认拒绝escape关闭和点击对话框之外自动关闭的对话框。options是json对象,类似下面:

{template: 'tplId',closeByEscape: false}

示例搭建

先看下我的简单示例。使用express generator创建一个新应用,或者直接使用Node.js开发入门——使用cookie保持登录里的LoginDemo示例。都成。

添加自己写的文件

有三个自己写的文件,ngdialog.html和serverTpl.html文件放在项目的public目录下,ngdialog.js放在public/javascripts下面。

ngdialog.html内容:

<!doctype html> <html ng-app="myApp"> <head> <title>use ngDialog in AngularJS</title> <link href='https://www.jb51.net/stylesheets/ngDialog-0.4.0.min.css' ><link/> <link href='https://www.jb51.net/stylesheets/ngDialog-theme-default-0.4.0.min.css' ><link/> <link href='https://www.jb51.net/stylesheets/ngDialog-theme-plain-0.4.0.min.css' ><link/> </head> <body ng-controller='myController'> <p><button type='button' ng-click='openDialog()'>Open Default</button></p> <p><button type='button' ng-click='openPlainDialog()'>Open Plain theme</button></p> <p><button type='button' ng-click='openDialogUseText()'>Open use text</button></p> <p><button type='button' ng-click='openModal()'>Open modal</button></p> <p><button type='button' ng-click='openUseExternalTemplate()'>Open use template on server</button></p> <p><button type='button' ng-click='openConfirmDialog()'>Open Confirm</button></p> <script src="https://www.jb51.net/javascripts/angular-1.4.3.min.js"></script> <script src="https://www.jb51.net/javascripts/ngDialog-0.4.0.min.js"></script> <script src="https://www.jb51.net/javascripts/ngdialog.js"></script> <!-- Templates --> <script type="text/ng-template"> <div><p>text in dialog</p></div> </script> </body> </html>

ngdialog.js内容:

angular.module('myApp', ['ngDialog']). controller('myController', function($scope,$rootScope, ngDialog){ $scope.template = '<div><p>text in dialog</p><p><button type="button">Button</button></p><div>'; //different template $scope.openDialog = function(){ ngDialog.open({template: 'firstDialogId'}); }; $scope.openPlainDialog = function(){ ngDialog.open({ template: 'firstDialogId', //use template id defined in HTML className: 'ngdialog-theme-plain' }); } $scope.openDialogUseText = function(){ ngDialog.open({ template: $scope.template, //use plain text as template plain: true, className: 'ngdialog-theme-plain' }); } $scope.openModal = function(){ ngDialog.open({ template: '<p>Text in Modal Dialog</p>', plain: true, className: 'ngdialog-theme-default', closeByEscape: false, closeByDocument: false }); } $scope.openUseExternalTemplate = function(){ ngDialog.open({ template: 'serverTpl.html', plain: false, className: 'ngdialog-theme-default', closeByEscape: false, closeByDocument: false }); }; $rootScope.userName = "ZhangSan"; $scope.openConfirmDialog = function(){ ngDialog.openConfirm({ template: '<div><h3>Please enter your name</h3><p>User Name:<input ng-model="userName"></input></p></div><div><button type="button" ng-click="closeThisDialog()">Cancel</button><button type="button" ng-click="confirm(userName)">Confirm</button></div>', plain: true, className: 'ngdialog-theme-default' }).then( function(value){ console.log('confirmed, value - ', value); }, function(reason){ console.log('rejected, reason - ', reason); } ); } //listen events $rootScope.$on('ngDialog.opened', function (e, $dialog) { console.log('ngDialog opened: ' + $dialog.attr('id')); }); $rootScope.$on('ngDialog.closed', function (e, $dialog) { console.log('ngDialog closed: ' + $dialog.attr('id')); }); });

serverTpl.html内容:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/3827f3d73d51872a92a078cacd1ab996.html