栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

将表单重置为原始状态(AngularJS 1.0.x)

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

将表单重置为原始状态(AngularJS 1.0.x)

没有解决方法的解决方案

我想出了一个使用AngularJS的解决方案,没有任何解决方法。这里的技巧是使用AngularJS功能来拥有多个具有相同名称的指令。

就像其他人提到的那样,实际上存在一个拉取请求(https://github.com/angular/angular.js/pull/1127),该请求进入了AngularJS
1.1.x分支,该分支允许重置表单。提交此拉取请求会更改ngModel和form /
ngForm指令(我希望添加一个链接,但是Stackoverflow不想让我添加两个以上的链接)。

现在,我们可以定义自己的ngModel和form / ngForm指令,并使用pull请求中提供的功能对其进行扩展。

我将这些指令包装在名为resettableForm的AngularJS模块中。您要做的就是将此模块包含到您的项目中,并且在这方面,AngularJS
1.0.x版的行为就像是Angular 1.1.x版一样。

“一旦您更新到1.1.x,您甚至不必更新代码,只需删除模块即可!”

该模块还通过所有添加到1.1.x分支中的表单重置功能测试。

您可以在我创建的jsFiddle(http://jsfiddle.net/jupiter/7jwZR/1/)中的示例中看到该模块的工作情况。

步骤1:在您的项目中包含resettableform模块

(function(angular) {// Copied from AngluarJSfunction indexOf(array, obj) {  if (array.indexOf) return array.indexOf(obj);  for ( var i = 0; i < array.length; i++) {    if (obj === array[i]) return i;  }  return -1;}// Copied from AngularJSfunction arrayRemove(array, value) {  var index = indexOf(array, value);  if (index >=0)    array.splice(index, 1);  return value;}// Copied from AngularJSvar PRISTINE_CLASS = 'ng-pristine';var DIRTY_CLASS = 'ng-dirty';var formDirectiveFactory = function(isNgForm) {    return function() {        var formDirective = { restrict: 'E', require: ['form'], compile: function() {     return {         pre: function(scope, element, attrs, ctrls) {  var form = ctrls[0];  var $addControl = form.$addControl;  var $removeControl = form.$removeControl;  var controls = [];  form.$addControl = function(control) {      controls.push(control);      $addControl.apply(this, arguments);  }  form.$removeControl = function(control) {      arrayRemove(controls, control);      $removeControl.apply(this, arguments);  }  form.$setPristine = function() {      element.removeClass(DIRTY_CLASS).addClass(PRISTINE_CLASS);      form.$dirty = false;      form.$pristine = true;      angular.forEach(controls, function(control) {          control.$setPristine();      });  }         },     }; },        };        return isNgForm ? angular.extend(angular.copy(formDirective), {restrict: 'EAC'}) : formDirective;    };}var ngFormDirective = formDirectiveFactory(true);var formDirective = formDirectiveFactory();angular.module('resettableForm', []).    directive('ngForm', ngFormDirective).    directive('form', formDirective).    directive('ngModel', function() {        return { require: ['ngModel'], link: function(scope, element, attrs, ctrls) {     var control = ctrls[0];     control.$setPristine = function() {         this.$dirty = false;         this.$pristine = true;         element.removeClass(DIRTY_CLASS).addClass(PRISTINE_CLASS);     } },        };    });})(angular);

步骤2:在您的控制器上提供一种重置模型的方法

请注意,重置表单时必须重置模型。在您的控制器中,您可以编写:

var myApp = angular.module('myApp', ['resettableForm']);function MyCtrl($scope) {    $scope.reset = function() {        $scope.form.$setPristine();        $scope.model = '';    };}

步骤3:将此方法包含在HTML模板中

<div ng-app="myApp"><div ng-controller="MyCtrl"><form name="form">    <input name="requiredField" ng-model="model.requiredField" required/> (Required, but no other validators)    <p ng-show="form.requiredField.$errror.required">Field is required</p>    <button ng-click="reset()">Reset form</button></form><p>Pristine: {{form.$pristine}}</p></div></dvi>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/484476.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号