如果我单击页面中的任何位置,当模式弹出窗口关闭时重置表单

2024-03-01

我正在使用 Angular 1.3。我在模态弹出窗口中有一个表单。提交表单后,我的模态弹出表单将被重置,如果我单击取消按钮,我的表单也会重置

    $scope.add_user =   function(add_form)
    {
        if(add_form.$valid)
        {
        $http({
           method:'POST',
           url:file_path,  
           headers:{'Content_Type':'appliaction/json'},
           data:$scope.text
        }).success(function(data){
           $scope.modalShown_add = ! $scope.modalShown_add; 
           $scope.modalShown_addsuccess = !$scope.modalShown_addsuccess; 
           $scope.getlist();
           add_form.reset();
        }).error(function(data){
            add_form.reset();
        })
    }
}

但是当我出现任何验证错误时,如果我单击页面的任何位置,我的模式弹出窗口在打开模式弹出窗口后会关闭,我无法重置我的表单。假设如果我在添加函数中传递表单名称来重置我收到错误的表格

 $scope.add  =function()
    {
       $scope.modalShown_add = ! $scope.modalShown_add; 
    }

Each form指令创建一个实例FormController所以你可以通过设置 name 属性来访问它,例如name="$ctrl.addForm".

要清除表单,您需要清除模型,然后使用表单控制器控制验证状态 https://code.angularjs.org/1.3.20/docs/api/ng/type/form.FormController您的表格(请参阅resetForm方法):

angular.module('myApp', [])
.controller('MyCtrl', ['$scope', function MyCtrl($scope) {
    var ctrl = this;
    
    ctrl.users = [];
    ctrl.showPopup = false;
    ctrl.openModal = openModal;
    ctrl.saveUser = saveUser;
    
    function openModal(user) {
      ctrl.showPopup = true;
      ctrl.user = angular.copy(user); // a copy of the user to disable 2-way binding with the list
      resetForm(ctrl.addForm);
    }
    
    function resetForm(form){
      form.$setPristine(); //set the form to its pristine state
      form.$setUntouched(); //set the form to its untouched state
    }
    
    function saveUser(user){
        //your saving logic here it is just a sample
        if (!user.id){
          user.id = ctrl.users.length;
          ctrl.users.push(user);
        } else {
          ctrl.users[user.id] = user;
        }
        
        //hide pop up
        ctrl.showPopup = false;
    }
    
    $scope.$ctrl = ctrl;
}])
.directive('modalDialog', function() {
  return {
    restrict: 'E',
    scope: {
      show: '='
    },
    replace: true, 
    transclude: true,
    link: function(scope, element, attrs) {
      scope.dialogStyle = {};
      if (attrs.width)
        scope.dialogStyle.width = attrs.width;
      if (attrs.height)
        scope.dialogStyle.height = attrs.height;
        if (attrs.overflow)
        scope.dialogStyle.overflow = attrs.overflow;
      scope.hideModal = function() {

        scope.show = false;
      };
    },
    template: "<div class='ng-modal' ng-show='show'><div class='ng-modal-overlay' ng-click='hideModal()'></div><div class='ng-modal-dialog' ng-style='dialogStyle'><div class='ng-modal-close' ng-click='hideModal()'><i class='fa fa-times-circle'></i></div><div class='ng-modal-dialog-content' ng-transclude></div></div></div>"// See below
  };
});
.ng-invalid.ng-touched {
  border: 1px solid red; 
}

.ng-modal{
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
}

.ng-modal-overlay{
  background-color: black;
  opacity: 0.3;
  z-index: 0;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
}

.ng-modal-dialog {
  position: relative;
  top: 50%;
  z-index: 1;
  background-color: white;
  padding: 1em;
  border: 1px solid gray;
}
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//code.angularjs.org/1.3.20/angular.js"></script>

<div ng-app="myApp">
  <div ng-controller="MyCtrl">
       <!-- send en empty object to create a new user -->
      <button ng-click="$ctrl.openModal({})">Show</button>
      
      <div>
        <a href="javasctript: void(0);" ng-repeat-start="u in $ctrl.users" ng-click="$ctrl.openModal(u)">{{u.name}}</a><span ng-repeat-end ng-if="!$last">, </span>
      </div>
      
      <modal-dialog show="$ctrl.showPopup">
          <form name="$ctrl.addForm" ng-submit="$ctrl.saveUser($ctrl.user)">
             <input name="user_name" ng-model="$ctrl.user.name" type="text" ng-required="true"/>
             <div>
                <button type="submit">Save</button>
                <button type="button" ng-click="$ctrl.showPopup = false;">Cancel</button>
             </div>
          </form>
      </modal-dialog>
  
  </div>
</div>

希望这对您有帮助。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如果我单击页面中的任何位置,当模式弹出窗口关闭时重置表单 的相关文章

随机推荐

  • mysql查询生成序列号

    我有一张表 student marks marks 44 55 64 98 76 预期输出 serial number marks 1 44 2 55 3 64 4 98 5 76 使用 mysql 用户定义的变量 可以使用查询来完成 se
  • 单元测试 Swagger 输出

    我在 ASP NET MVC WebAPI 项目中使用 Swagger 该项目安装了 Swashbuckle nugget 包并生成 Swagger UI 和 Swagger docs v1 我一直遇到的一个问题是 开发人员会因为不仔细命名
  • Python 中的基本日志记录 dictConfig

    NOTE我知道这个答案 https stackoverflow com questions 7507825 python complete example of dict for logging config dictconfig但这对我不
  • 在 CSV 导出中输出列标题

    我有一个导出到 csv 文件的查询 它工作得很好 唯一我不明白的是我还需要导出列标题 并将它们显示为全名 用户名 标志和原因 下面是代码 它可以很好地导出所有行 但我不确定如何导出受尊重的行上方的列标题 header Content typ
  • 如何让 ocaml 相信两个函子实例化是相等的

    假设我有许多模块 它们都使用一种模块类型进行参数化 并且彼此之间也具有依赖关系 module type AT sig end module B A AT struct module Hash struct type t int let eq
  • 调试 R 中的通用函数

    如何调试通用函数 使用 debug 或调试包中的 mtrace 举个例子 我想调试 NADA 包中的 cenreg 特别是采用公式输入的方法 您可以像这样检索方法详细信息 library NADA getMethod cenreg c fo
  • 在 iPhone 上用手指画直线

    背景 我正在尝试创建一个非常简单的 iPhone 应用程序 允许用户用手指在屏幕上绘制多条直线 我在我的程序中使用这两种方法UIViewController捕获每条线端点的坐标 void touchesBegan NSSet touches
  • 通过 Retrofit 将图像文件从 Android 发送到 Spring

    我正在尝试通过 Spring 将 Image 通过 Retrofit 上传到 S3 所以 这基本上是两个任务 将图像传递给 Spring API 从 Spring API 将其上传到 S3 服务器 我知道如何将图像上传到 S3 并且工作正常
  • Xcode 9 中未检测到 GoogleMobileAds (Admob) 框架 - 链接器错误

    我收到引用自的链接器错误 GADInterstitial 架构arm64的未定义符号 我使用最新的admob框架GoogleMobileAdsSdkiOS 7 24 0 请参阅附图 如何修复 iOS 11 的问题 这是 Xcode 9 的错
  • 是否可以在进程运行时停止 libxml 解析器?

    我正在使用 libxml 解析器来快速下载和显示 我的问题是在某些情况下我需要在解析器处理时停止它 可以做吗 你们能给我建议吗 提前致谢 塞卡尔 贝塔拉姆 SAX2 libxml2 API 具有xmlStopParser 在这种情况下可能有
  • 当 TabBarView 子项滚动时使 SliverAppBar 可滚动

    我的主屏幕带有底部导航 由两个项目组成 其中所有项目都有 ListView 和无限列表 并且我希望当用户在其中一个列表中滚动时 SliverAppBar 可以滚动 这是我到目前为止所拥有的 class HomeScreen extends
  • 在 Java 中从 Windows 1252 转换为 UTF8:使用 CharsetDecoder/Encoder 处理空字符

    我知道这是一个非常普遍的问题 但我变得很生气 我使用了这段代码 String ucs2Content new String bufferToConvert inputEncoding byte outputBuf ucs2Content g
  • pyglet:按下按钮时更改精灵实例的图像

    此代码显示图像assassin1 png在黑色背景上 一旦我按下按键 图像就会向右移动 一旦松开按键 图像就会停止移动 一旦我按下该键 它也应该更改为图像assassin2 png当我松开按键时 它应该变回assassin1 png 然而
  • onStop() 触发时我必须保留 Activity 数据吗?

    我找到了有关的官方文档活动生命周期 http developer android com training basics activity lifecycle stopping html不清楚 它充满了诸如此类的词should and mi
  • 如何使用 webdriver 获取文本区域的文本内容?

    我正在尝试获取 a 的内容textarea http www w3 org TR html4 interact forms html h 17 7在 HTML 表单中使用Python 中的网络驱动程序 http code google co
  • 从 wp7 启动另一个应用程序

    我想将一组启动器放置到我的应用程序中的一系列应用程序中 有没有办法在单击图标时从 Windows Phone 7 启动另一个应用程序 如果这是不可能的 是否有办法在我的应用程序中嵌入其他应用程序的启动器以达到相同的结果 No 没有办法如何在
  • VB.NET 中的 ToString 与 ToString()

    在 VB NET 中使用 ToString 和 ToString 有什么区别 没有什么 VB NET 允许您排除任何不接受参数的方法上的括号
  • 字符串错误“常量字符串太长”

    有一个10万字符的文本需要显示 如果我将它放入 String 对象中 则会收到错误 常量字符串太长 StringBuffer 对象也是如此 StringBuffer stringBuffer new StringBuffer stringB
  • 通过索引中的部分字符串匹配选择行

    有一个这样的系列 ds Series wikipedia 10 wikimedia 22 wikitravel 33 google 40 google 40 wikimedia 22 wikipedia 10 wikitravel 33 d
  • 如果我单击页面中的任何位置,当模式弹出窗口关闭时重置表单

    我正在使用 Angular 1 3 我在模态弹出窗口中有一个表单 提交表单后 我的模态弹出表单将被重置 如果我单击取消按钮 我的表单也会重置 scope add user function add form if add form vali