


This time I will bring you a detailed explanation of the implementation steps of using the dialog box ngDialog in Node.js. What are the precautions for using the dialog box ngDialog in Node.js? The following is a practical case, let's take a look.
When building a website, we often encounter situations where a dialog box pops up to obtain user input or a dialog box pops up to ask the user to confirm an operation. There is an extension module based on AngularJS that can help us accomplish this kind of thing elegantly: ngDialog.
ngDialog provides an example web page on github that demonstrates its various uses, here: https://github.com/likeastore/ngDialog/blob/master/example/index.html. The readme of ngDialog's github homepage also provides a more detailed introduction to commonly used instructions and services, which you can refer to. My article is purely based on the ngDialog example.
Create a dialog box using ngDialog.open(options) or ngDialog.openConfirm(options). The former opens a normal dialog box, and a series of attributes such as themes and templates can be specified through options. The latter opens a dialog box that by default refuses escape to close and automatically closes outside of the dialog box. options is a json object, similar to the following:
{template: 'tplId',closeByEscape: false}
Example setup
Let’s take a look at my simple example first. Use the express generator to create a new application, or directly use the LoginDemo example in Getting Started with Node.js Development - Using Cookies to Stay Logged In. Everything is done.
Add files written by yourself
There are three files written by yourself. The ngdialog.html and serverTpl.html files are placed in the public directory of the project, and ngdialog.js is placed in the public directory of the project. Under public/javascripts.
ngdialog.html content:
nbsp;html> <title>use ngDialog in AngularJS</title> <link><link> <link><link> <link><link> <p><button>Open Default</button></p> <p><button>Open Plain theme</button></p> <p><button>Open use text</button></p> <p><button>Open modal</button></p> <p><button>Open use template on server</button></p> <p><button>Open Confirm</button></p> <script></script> <script></script> <script></script> <!-- Templates --> <script> <p><p>text in dialog </script>
ngdialog.js content:
angular.module('myApp', ['ngDialog']). controller('myController', function($scope,$rootScope, ngDialog){ $scope.template = '<p></p><p>text in dialog</p><p><button>Button</button></p><p>'; //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><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: '<p></p><h3 id="Please-enter-your-name">Please enter your name</h3><p>User Name:<input></p><p><button>Cancel</button><button>Confirm</button></p>', 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 content:
nbsp;html> <title>A Server Template for ngDialog</title> <p> </p><h3 id="Server-Template-for-ngDialog">Server Template for ngDialog</h3>
Introducing ngDialog
To use ngDialog, you need to use script to introduce the corresponding js library file in HTML. In addition, several css files must be introduced in the head part. Just refer to ngdialog.html.
ngDialog library files can be downloaded from https://github.com/likeastore/ngDialog, or downloaded here: http://cdnjs.com/libraries/ng-dialog. I renamed the file in version 0.4.0 under the link below. The renamed files are as follows:
ngDialog-0.4.0.min.js
ngDialog-0.4.0.min. css
ngDialog-theme-default-0.4.0.min.css
ngDialog-theme-plain-0.4.0.min. css
API summary learning
I encountered some doubts when learning, which are recorded below.
Dialog content template
To display a dialog box, you must specify the content to be displayed. This is specified via the template attribute. There are three cases of template:
-
Plain text template embedded in js or html code. At this time, you need to set the plain attribute to true in options at the same time, that is, "plain: true", and then Directly assign a piece of html code to the template, for example, template:
Text in ngDialog
-
Define the template template in HTML, and assign the id to the template. Assign a value to the template option, such as "template: 'templateId'". The template may look like this:
> ; Use an external html fragment (file) as a template, such as "template: 'serverTpl.html'", the serverTpl.html file is on the server.
Specify the theme
You can specify the theme through className in options. Currently there are ngdialog-theme-default and ngdialog -theme-plain two themes. These two notes correspond to two css files, which we have introduced through HTML earlier.
Response to closing and other events
When the dialog box is closed, some events will be emitted, and developers can listen to these events to get notifications. The specific events are:
ngDialog.opened
ngDialog.closing
ngDialog.closed
这些事件定义在$rootScope服务里,所以我们的controller构造函数必须依赖$rootScope。比如我们现在的模块定义和controller定义:
angular.module(‘myApp', [‘ngDialog']). controller(‘myController', function(scope,scope,rootScope, ngDialog){
在模块定义里注明依赖ngDialog模块,在controller定义里注入了$rootScope和ngDialog。
如何监听事件,看ngdialog.js代码吧。
另外我们还可以在options中设置preCloseCallback,指定一个函数,这个函数在对话框取消关闭之前会调用到。https://github.com/likeastore/ngDialog这里有说明。注意,是取消对话框时会调用到,如果确认,不会调到哦。所以,这个preCloseCallback通常在阻止或提醒用户放弃输入时使用,比如用户注册,输入了一些信息,想退,你可以问他是否要真的想放弃。
指定对话框的controller
可以通过options设置controller属性来给一个对话框指定控制器。这个控制器可以是内联(inline)的:
$scope.openInlineController = function () { $rootScope.theme = 'ngdialog-theme-plain'; ngDialog.open({ template: 'withInlineController', controller: ['$scope', '$timeout', function ($scope, $timeout) { var counter = 0; var timeout; function count() { $scope.exampleExternalData = 'Counter ' + (counter++); timeout = $timeout(count, 450); } count(); $scope.$on('$destroy', function () { $timeout.cancel(timeout); }); }], className: 'ngdialog-theme-plain' }); };
也可以是在js中定义的。比如我们在js里定义了一个名为“InsideCtrl”的controller,就可以在调用ngDialog.open(options)时在options里设置controller属性:
$scope.openInsideController = function(){ ngDialog.open({ template: "serverTpl.html", className: "ngdialog-theme-plain", controller: "InsideCtrl" }); };
具体示例可以参考:https://github.com/likeastore/ngDialog/blob/master/example/index.html。
确认对话框
比如让用户确认删除,让用户输入。使用openConfirm(options)就可以创建这样的对话框。ngDialog向$scope注入了两个函数,一个是confirm(value),一个是closeThisDialog(reason),分别用来确认关闭对话框,取消关闭对话框。将它们关联到确认和取消按钮上,就可以确认、取消对话框。
假如我要让用户输入用户名,可以用ng-model指令将作用域内某个变量和input绑定,在调用confirm时传入绑定的变量,这样就可以在confirm中拿到用户填写的值来做进一步处理。我们的示例中的openConfirmDialog按钮,点击后就弹出一个让用户输入名字的对话框,当用户输入完毕,点击Confirm按钮时,我们可以通过confirm方法的value参数获得用户名输入框的值。
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
The above is the detailed content of Detailed explanation of the steps to implement the dialog box ngDialog in Node.js. For more information, please follow other related articles on the PHP Chinese website!

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Zend Studio 13.0.1
Powerful PHP integrated development environment

Atom editor mac version download
The most popular open source editor

SublimeText3 Chinese version
Chinese version, very easy to use