Angular 使用 ng-model 时 JS 从控制器清空表单输入字段

Angular JS emptying form input field from controller when using ng-model

我有一个非常简单的表单,其中包含几个输入字段。这是一个:

            <label class="item item-input item-stacked-label">
                <span class="input-label">First Name</span>
                <input type="text" ng-model="signInData.firstName" placeholder="">
            </label>

当我提交表单并且它从 AJAX 请求中 returns 为真时,我想简单地清空输入字段,但它不起作用。我可能遗漏了一些简单的东西..

这是我尝试清空的控制器代码:

        $scope.signInData.firstName = '';

这是从范围中清空模型,我可以看到,因为我正在控制台记录范围对象,但它不会清空值。

我用 Google 搜索了一段时间,看到了类似 $setPristine 方法等的东西,但是 none 也可以。

编辑

    $http({
      url: 'sign_in_app',
      method: 'POST',
      data: {'firstName': signInData.firstName}
    }).then(function(response) {
      if(response.data.response == 'error'){
        $scope.errorSignIn = 'Sorry, there was an error signing in.';
      }else{
        $scope.errorSignIn = null;
        $scope.signInData.firstName = '';
        $scope.signInForm = null;
      }
    }

编辑 - 表格

    <form ng-submit="signIn(signInData)" id="signInForm" name="signInForm">
        <div ng-bind-html="errorSignIn" class="center error"></div>
        <div class="list">
            <label class="item item-input item-stacked-label">
                <span class="input-label">First Name</span>
                <input type="text" ng-model="signInData.firstName" placeholder="">
            </label>

            <label class="item">
              <button class="button button-full button" type="submit">Sign in</button>
            </label>
         </div>
     </form>

编辑 3 - 更新 HTTP 调用

    $http({
      url: '/sign_in/sign_in_app',
      method: 'POST',
      data: {'firstName': $scope.signInData.firstName, 'lastName': $scope.signInData.lastName}
    }).then(function(response) {
      if(response.data.response == 'error'){
        $scope.errorSignIn = 'Sorry, there was an error signing in.';
      }else{
        $scope.errorSignIn = null;
        $scope.signInData.firstName = '';
        $scope.signInForm = null;
      }

    }

$setPristine 不清理输入字段。您可以尝试使用 $scope.signInData = {}; 提交表格后

试试这个:从 ng-submit 中删除 signInData,将 $scope.signIn() 方法更改为不再需要它,使用 $scope.signInData 对象设置所有值对于您的 $http 调用(顺便说一句,它可能应该用于使您的控制器更干净并遵循典型的 Angular 设计模式的服务),然后查看您的表单是否仍然存在问题输入未被清除。

我的怀疑是,因为您从那一点开始超过 signInData,您开始对范围对象的副本进行操作,这就是为什么清除属性的行为不像您期望的那样。