【问题标题】:Parent ngForm dirty status based on children forms基于子表单的父 ngForm 脏状态
【发布时间】:2016-06-14 23:13:39
【问题描述】:

我在尝试更新我的父 ngForm 状态时遇到了一些问题;当它的所有孩子都被清理并设置为“Pristine”时,它应该设置为“Pristine”......但这似乎不会自动发生。

我在这里创建了一个 plunk 来更好地解释这个问题:http://plnkr.co/edit/vCX7ltOb8fgl3fkEpvzy?p=preview

<body ng-controller="MainCtrl">

    <div ng-form="parentForm1" class="parent-form">
      parentForm1.dirty: <b>{{parentForm1.$dirty}}</b>

      <form name="childForm1" class="child-form" novalidate>
        childForm1.dirty: <b>{{childForm1.$dirty}}</b>
        <br/>
        <input type="text" ng-model="field1">
        <br/>
        <button ng-click="reset1()">Clean and setPristine</button>
      </form>

      <form name="childForm2" class="child-form" novalidate>
        childForm2.dirty: <b>{{childForm2.$dirty}}</b>
        <br/>
        <input type="text" ng-model="field2">
        <br/>
        <button ng-click="reset2()">Clean and setPristine</button>
      </form>

    </div>

  </body>

我哪里错了?我找到了使用外部模块的解决方案...我想用尽可能少的代码来解决它(也许是指令?)。

【问题讨论】:

  • 我想你找到了你自己的答案,用'angular-input-modified'!请添加解决方案并将其标记为已回答,以便与社区分享。

标签: angularjs angularjs-ng-form


【解决方案1】:

我用“angular-input-modified”找到了自己的答案,这是更新后的 plunk:http://plnkr.co/edit/vCX7ltOb8fgl3fkEpvzy?p=preview

HTML:

<!DOCTYPE html>
<html ng-app="test">

<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
  <script src="//rawgit.com/betsol/angular-input-modified/master/dist/angular-input-modified.js"></script>
  <script src="script.js"></script>
  <link rel="stylesheet" href="style.css" />
</head>

<body ng-controller="MainCtrl">

  <div ng-form="parentForm1" class="parent-form">
    parentForm1.dirty: <b>{{parentForm1.$dirty}}</b>
    <br/>
    parentForm1.modified: <b>{{parentForm1.modified}}</b>

    <form name="childForm1" class="child-form" novalidate>
      childForm1.dirty: <b>{{childForm1.$dirty}}</b>
      <br/>
      <input type="text" ng-model="field1">
      <br/>
      <button ng-click="reset1()">Clean and setPristine</button>
    </form>

    <form name="childForm2" class="child-form" novalidate>
      childForm2.dirty: <b>{{childForm2.$dirty}}</b>
      <br/>
      <input type="text" ng-model="field2">
      <br/>
      <button ng-click="reset2()">Clean and setPristine</button>
    </form>

  </div>

  <form name="exChildForm1" class="child-form" novalidate>
    exChildForm1.dirty: <b>{{exChildForm1.$dirty}}</b>
    <br/>
    <input type="text" ng-model="exfield1">
    <br/>
    <button ng-click="exreset1()">Clean and setPristine</button>
  </form>

</body>

</html>

JS:

var app = angular.module('test', ['ngInputModified']);

app.controller('MainCtrl', function($scope) {

  $scope.reset1 = function() {
    $scope.field1 = null;
    $scope.childForm1.$setPristine();
  }

  $scope.reset2 = function() {
    $scope.field2 = null;
    $scope.childForm2.$setPristine();
  }

  $scope.exreset1 = function() {
    $scope.exfield1 = null;
    $scope.exChildForm1.$setPristine();
  }

});

【讨论】:

    猜你喜欢
    • 2018-06-20
    • 2014-10-14
    • 2016-05-06
    • 1970-01-01
    • 2020-04-06
    • 2017-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多