1

我正在使用 ngDialog [ https://github.com/likeastore/ngDialog ] 创建模式,并且我有一个基本表单,当我单击按钮时会发出 POST 请求。在同一个按钮单击上,我希望清除表单输入。通常你可以做类似的事情,$scope.valueOfNgModel = "";但在这种情况下这不起作用。此外,ngDialog 范围与它实例化控制器的 $scope 相同。

这是 ngDialog 实例化:

$scope.addStudents = function() {
  ngDialog.open({
    template: 'addStudents',
    scope: $scope
  });
};

这是 ngDialog 模板:

<script type="text/ng-template" id="addStudents">
  <h1> Add Students to Your Class </h1>

  <form ng-model="myForm" ng-submit="addNewStudent(newStudent)">
    <input ng-model="newStudent.firstName" placeholder="First Name">
    <input ng-model="newStudent.lastName" placeholder="Last Name">
    <input ng-model="newStudent.email" placeholder="Email">
    <input ng-model="newStudent.image" placeholder="imageUrl">
    <h3> Note: Student Password by Default will be Firstname.lastname </h3>

    <button class="ngdialog-button 
        ngdialog-button-primary" 
        type="submit">Add Student
    </button>
</form>

</script>

这是在我想清除所有输入字段的表单提交(或按钮单击)上运行的函数:

$scope.addNewStudent = function(student) {

  var newUserToAdd = {
    firstName: student.firstName,
    lastName: student.lastName,
    teacher: false,
    email: student.email,
    password: student.firstName + '.' + student.lastName,
    classesBelongTo: [$scope.currentClassId],
    image: student.image
  };

  userService.postNewUser(newUserToAdd)
    .then(function(response) {
      $scope.loggedInUser = response.data;
    })
    console.log("this is $scope.newStudent", $scope.newStudent); //this is undefined - I'm not sure why

    $scope.newStudent = {}; //I tried this to clear it out
    $scope.$apply(); //I tried this to no avail
}

任何帮助是极大的赞赏!

4

1 回答 1

2

你可以给你的表格起个名字,然后打电话$scope.yourFormName.$setPristine();

示例:小提琴

于 2016-01-14T14:15:44.840 回答