当前位置:   article > 正文

Anglurjs之弹出模态框_angular 监听鼠标事件弹出模态框

angular 监听鼠标事件弹出模态框

实现功能:新增弹出模态框,编辑弹出模态框,并将主页面数据带到模态框,保存提交表单。

主要有几个点,怎么将模态框显示出来,怎么传参,怎么获取模态框中文本框的值,编辑时文本框怎么带入值

说得再多都不如代码直观。

下面是html页面

  1. <button class="btn m-b-xs btn-sm btn-primary btn-addon" data-toggle="modal" data-target="#matchRuleDialog" id="addDict" ng-click="openModal('add')"><i class="fa fa-plus"></i>新增</button>
  2. <button class="btn m-b-xs btn-sm btn-primary btn-addon" id="editDict" ng-click="openModal('edit')"><i class="fa fa-plus"></i>编辑</button>

这个是新增和编辑按钮,用来弹出模态框。 ng-click="openModal(),里面的参数edit和add是用于js中 辨别新增还是编辑的。

下面是模态框,两段代码放在同一个HTML页面

  1. <script type="text/ng-template" id="modal.html">
  2. <div class="hbox" id="matchRuleDialog" data-backdrop="static"
  3. data-keyboard="false" aria-hidden="true">
  4. <div >
  5. <div class="modal-content">
  6. <div class="modal-header">
  7. <button type="button" class="close" ng-click="cancel()">
  8. <span>&times;</span>
  9. </button>
  10. </div>
  11. <div class="modal-body">
  12. <form name="form-contact" class="form-validation" ng-submit="submitForm()">
  13. <input type="hidden" ng-model="dict.dictId" >
  14. <input type="hidden" name="matchItemValue" ng-model="dict.parentId">
  15. <div class="form-group">
  16. <label class="control-label col-lg-3 col-sm-3 col-xs-3"
  17. for="matchItemValue-input"> <span
  18. id="tv-iui-matchItemValueLabel">数据字典编码</span> <span
  19. class="required">*</span>
  20. </label>
  21. <div class="col-lg-7 col-sm-7 col-xs-7">
  22. <input type="text" class="form-control" id="dictCode"
  23. name="matchItemValue" ng-model="dict.dictCode" required>
  24. <span style="color: red"
  25. ng-show="mainCtrl.matchRuleForm.matchItemValue.$dirty && mainCtrl.matchRuleForm.matchItemValue.$invalid">
  26. <span
  27. ng-show="mainCtrl.matchRuleForm.matchItemValue.$error.required"
  28. id="tv-iui-requiredWarning">必填项</span>
  29. </span>
  30. </div>
  31. </div>
  32. <div class="form-group">
  33. <label class="control-label col-lg-3 col-sm-3 col-xs-3"
  34. for="promiseSpeed-input"> <span
  35. id="tv-iui-promiseSpeedLabel">数据字典名称</span> <span
  36. class="required">*</span>
  37. </label>
  38. <div class="col-lg-7 col-sm-7 col-xs-7">
  39. <input type="text" class="form-control" id="promiseSpeed-input"
  40. name="promiseSpeed" ng-model="dict.dictName" required>
  41. <span style="color: red"
  42. ng-show="mainCtrl.matchRuleForm.promiseSpeed.$dirty && mainCtrl.matchRuleForm.promiseSpeed.$invalid">
  43. <span
  44. ng-show="mainCtrl.matchRuleForm.promiseSpeed.$error.required"
  45. id="tv-iui-requiredWarning">必填项</span> <span
  46. ng-show="mainCtrl.matchRuleForm.promiseSpeed.$error.pattern"
  47. id="tv-iui-patternWarning">不符合规则</span>
  48. </span>
  49. </div>
  50. </div>
  51. <div class="form-group">
  52. <label class="control-label col-lg-3 col-sm-3 col-xs-3"
  53. for="promiseBandwidth-input"> <span
  54. id="tv-iui-promiseSizeLabel">数据字典类型</span> <span
  55. class="required">*</span>
  56. </label>
  57. <div class="col-lg-7 col-sm-7 col-xs-7">
  58. <input type="text" class="form-control"
  59. id="promiseBandwidth-input" name="promiseSize"
  60. ng-model="dict.dictType" required> <span
  61. style="color: red"
  62. ng-show="mainCtrl.matchRuleForm.promiseSize.$dirty && mainCtrl.matchRuleForm.promiseSize.$invalid">
  63. </span>
  64. </div>
  65. </div>
  66. <div class="form-group">
  67. <label class="control-label col-lg-3 col-sm-3 col-xs-3" for="promiseSpeed-input"> <span
  68. id="tv-iui-peakSpeedLabel">备注</span>
  69. </label>
  70. <div class="col-lg-7 col-sm-7 col-xs-7">
  71. <input type="text" class="form-control" id="peakSpeed-input"
  72. name="peakSpeed" ng-model="dict.remark" >
  73. </div>
  74. </div>
  75. <div class="modal-footer">
  76. <button type="submit" class="btn btn-success" id="saveButton">保存</button>
  77. <button class="btn btn-default" ng-click="cancel()"
  78. id="tv-iui-matchRule-cancle">取消</button>
  79. </div>
  80. </form>
  81. </div>
  82. </div>
  83. </div>
  84. </div>
  85. </script>

<script type="text/ng-template" id="modal.html"> 指名ng模板,id是实际使用模板的引用,标签中的内容才是真正的模板。

data-backdrop="static" 是点击背景空白处窗口不关闭,data-keyboard="false"触发ESC事件时不关闭, aria-hidden="true"

下面是js代码,用于打开模态框,在主页面的app.controller的参数必须有$modal

  1. //打开模态框
  2. $scope.openModal = function(add) {
  3. var modalInstance = $modal.open({
  4. templateUrl : 'modal.html',//script标签中定义的id
  5. controller : 'modalCtrl',//modal对应的Controller
  6. resolve : {
  7. data : function() {//data作为modal的controller传入的参数
  8. return $scope.dict.Id+","+add;//用于传递数据
  9. }
  10. }
  11. })
  12. }
  1. app.controller('modalCtrl', function($scope, $modalInstance, data,$http,$state) {
  2. var strs= new Array(); //定义一数组
  3. strs=data.split(","); //字符分割
  4. $scope.data= data;
  5. $scope.dict={};
  6. //在这里处理要进行的操作
  7. $http({
  8. method: 'post',
  9. url: '/Dict/getDictByPrimaryKey',
  10. params: {
  11. dictId: strs[0]
  12. },
  13. headers: {
  14. 'Content-Type': 'application/x-www-form-urlencoded'
  15. }
  16. }).then(function(resp) {
  17. if(strs[1]=="add")
  18. {
  19. //执行某些操作
  20. }
  21. if(strs[1]=="edit")
  22. {
  23. //给模态框中的文本框赋值
  24. $scope.dict.dictCode = resp.data.data.dictCode;
  25. $scope.dict.dictName = resp.data.data.dictName;
  26. $("#promiseBandwidth-input").attr("readonly",true);
  27. $scope.dict.dictType = resp.data.data.dictType;
  28. },
  29. function(resp) {
  30. });
  31. $scope.ok = function() {//点击确定按钮关闭模态框
  32. $modalInstance.close();
  33. };
  34. $scope.cancel = function() {取消关闭
  35. $modalInstance.dismiss('cancel');
  36. }
  37. // 保存菜单信息
  38. $scope.submitForm = function() {
  39. $http({
  40. method: 'post',
  41. url: '/Dict/saveDict',
  42. data: $.param($scope.dict),
  43. headers: {
  44. 'Content-Type': 'application/x-www-form-urlencoded'
  45. }
  46. }).then(function(resp) {
  47. if(resp.data.code == '0') {
  48. // 关闭也面
  49. $modalInstance.close();
  50. $state.reload();
  51. } else {
  52. layer.msg(resp.data.message);
  53. }
  54. }, function(resp) {
  55. });
  56. };

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/641259
推荐阅读
相关标签
  

闽ICP备14008679号