提交时不会清除文本框,选择等输入字段

时间:2016-11-12 19:13:42

标签: angularjs-ng-model

var app = angular.module('myapp', ['ngMaterial']);
app.controller('nameController', ['$scope', function($scope) {
  alert("controller called");
   $scope.addNewName = function() 
   {
    $scope.showSection = true;
  };

  $scope.submit = function() {
    alert("Added");
    $scope.adduserform.aname = "";
    $scope.showSection = false;
  }

}]);
<html ng-app="myapp">
<head>
   <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/angular_material/1.1.0/angular-material.min.css">
</head>
<body>
<div layout="row" ng-controller="nameController">
  <div>
    <md-button aria-label="add" type="button" class="md-primary md-raised" ng-click="addNewName()">Add New Name</md-button>
  </div>

  <!-- below section will be shown when on click on Add New Name button -->
  <form ng-show="showSection" name="adduserform">
    <label>Enter Name: </label>
    <md-input-container>
      <input type="text" ng-model="adduserform.aname" aria-label="name" id="aname" required/>
    </md-input-container>
    <md-button type="button" aria-label="submit"  class="md-primary md-raised" ng-click="submit()">Submit</md-button>
  </form>
</div>
  <!-- Angular Material requires Angular.js Libraries -->
  <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-animate.min.js"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-aria.min.js"></script>
 
  <!-- Angular Material Library -->
  <script src="http://ajax.googleapis.com/ajax/libs/angular_material/1.1.0/angular-material.min.js"></script>
   
  
   
</body>
</html>

提交时不会清除文本框,选择等输入字段。这些字段由ng-model引用,并在代码中用作$scope.modelname。点击提交后 - 我用$scope.modelname=''清除它们 当我再次打开表单而不刷新页面时 - 字段以红色突出显示 假设表单类似于添加用户表单。当我点击第一个用户的提交并再次重新打开表单以添加另一个用户。当我重新打开表单时 - 它会突出显示该字段。我知道它,因为该字段是根据需要提到的,因此它会产生此错误。当我重新打开表单时,我应该如何清除它们并确保字段没有突出显示?请帮帮我。

1 个答案:

答案 0 :(得分:0)

尝试提供$ scope.modelname = {}; 或者你可以尝试这种方式,范围。$$ nextSibling.modelname ='';

如果这有帮助,请告诉我