Как я могу очистить эту форму AngularJS после отправки данных? - PullRequest
1 голос
/ 02 ноября 2019

Я работаю над приложением блога с Codeigniter 3.1.8 и AngularJS v1.7.8 .

Пост Форма комментариев отправлена ​​через AngularJS. Вот форма:

<form name="commentForm" novalidate>
    <div class="row uniform">
        <div class="form-controll 6u 12u$(xsmall)">
            <input type="text" name="name" id="name" ng-model="newComment.name" placeholder="Name" ng-required="true" />
            <span class="error" ng-show="(commentForm.name.$touched && commentForm.name.$invalid) || (commentForm.$submitted && commentForm.name.$invalid)">This field can not be empty</span>
        </div>
        <div class="form-controll 6u$ 12u$(xsmall)">
            <input type="email" name="email" id="email" ng-model="newComment.email" placeholder="Email" ng-required="true" />
            <span class="error" ng-show="(commentForm.email.$touched && commentForm.email.$invalid) || (commentForm.$submitted && commentForm.email.$invalid)">Enter a valid email address</span>
        </div>
        <div class="form-controll 12u$">
            <textarea name="comment" rows="6" id="message" ng-model="newComment.comment" placeholder="Comment" ng-required="true"></textarea>
            <span class="error" ng-show="(commentForm.comment.$touched && commentForm.comment.$invalid) || (commentForm.$submitted && commentForm.comment.$invalid)">This field can not be empty</span>
        </div>
        <!-- Break -->
        <div class="12u$">
            <input type="submit" value="Add comment" ng-click="createComment()" class="button special fit" />
        </div>
    </div>
</form>

Вот контроллер AngularJS , который управляет его передачей на серверный модуль Codeigniter:

 // Post comment
.controller('PostCommentController', ['$scope', '$http', '$routeParams', function($scope, $http, $routeParams) {
    const slug = $routeParams.slug;
    $http.get('api/' + slug).then(function(response) {

        let post_id = response.data.post.id

        $scope.newComment = {
            slug: $routeParams.slug,
            post_id: post_id,
            name: $scope.name,
            email: $scope.email,
            comment: $scope.comment
        };

        $scope.createComment = function(){
          $http.post('api/comments/create/' + post_id, $scope.newComment);
        };
    });
}])

Я еще не былсмог найти способ очистить форму и сделать ее нетронутой после (и только после) отправленных данных.

Ответы [ 3 ]

1 голос
/ 02 ноября 2019

Также установите форму как нетронутой : $scope.commentForm.$setUntouched().

1 голос
/ 02 ноября 2019

Примерно так:

$scope.createComment = function(){
  $http.post('api/comments/create/' + post_id, $scope.newComment)
    .then(() => {
       $scope.newComment = {
         slug: $routeParams.slug,
         post_id: '',
         name: '',
         email: '',
         comment: ''
      };
  });
};
0 голосов
/ 02 ноября 2019

Вот как это работает:

$scope.createComment = function() {
    $http.post('api/comments/create/' + post_id, $scope.newComment)
        .then(() => {
            $scope.newComment = {};
            $scope.commentForm.$setPristine();
            $scope.commentForm.$setUntouched();
        });
};
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...