首页 > 语言 > JavaScript > 正文

AngularJS模态框模板ngDialog的使用详解

2024-05-06 15:34:23
字体:
来源:转载
供稿:网友

项目开始的时候我们用的对话框是AngularJS的$modal模态框,但是后来发现$modal打开的对话框是相对页面静止的,如果对话框是一个很长的表单,这样体验度就不是很好了,还有$modal传$scope不是很灵活的原因,后来就改用的$ngDialog.

官方的API在这里:https://www.npmjs.com/package/ng-dialog

首先确定你的项目已经安装了$ngDialog需要的相关文件。

接下来一个简单的demo

del.html内容如下,就是你的对话框内容,这里比较简单,只是一个确认对话框

<meta charset="UTF-8"> <div class="modal-header">  <h4 class="modal-title">删除</h4> </div>  <div class="modal-body">  <form autocomplete="off" class="file-brief file-brief-show form-validation" name="ObsForm" id="form-new-style">   <div class="col-sm-12 m-t-xs m-b-xs ">    <div class="form-group">     <label>您确认要删除吗?</label>    </div>   </div>  </form> </div> <div class="modal-footer">  <button type="submit" class="btn" ng-click="confirm()" >确定</button>  <button type="button" class="btn" ng-click="cancel()">取消</button> </div> 

在你的Controller里添加你的方法:

$scope.del = function () {    ngDialog.open({     template: '/del.html',     className: 'ngdialog-theme-default',     scope: $scope,     controller: function ($scope) {     ...      $scope.confirm = function () {      ...      };      $scope.cancel = function () {       $scope.closeThisDialog();      };     }    }); }; 

这里template里是一个路径,其实如果对话框简单的话可以在template里直接写<div>内容,只是要加一个属性:plain:true,

对话框的高度宽度都可以自定义,width:500,//绝对宽度。或者width:‘%50' //相对宽度

针对以上两点,示例:

$scope.delBucket = function () {    ngDialog.open({     template: '<div class="modal-header"><h4 class="modal-title">删除Bucket</h4></div>' +     '<div class="modal-footer"><button type="submit" class="btn" ng-click="confirm()" >确定</button>'+        '<button type="button" class="btn" ng-click="cancel()">取消</button></div>',   plain:true,     className: 'ngdialog-theme-default',   width:600,     scope: $scope,     controller: function ($scope) {     ...      $scope.confirm = function () {      ...      };      $scope.cancel = function () {       $scope.closeThisDialog();      };     }    });   }; 

以上只是一个简单的示例,官方文档上还有通过id打开对话框,打开一个确认对话框等相关详细介绍。

另外有一篇对$ngDialog介绍非常详细的文章,基本上就是把官方API翻译过来了。

附上文章链接//www.Vevb.com/article/139899.htm

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选