'AngularJS: TypeError: Cannot read properties of undefined (reading 'parse') after reset form values

I need to reset an AngularJS form after submitting and receiving a response. But after that I can't use the form, I get an error when I try to enter in any field: "Cannot read properties of undefined (reading 'parse')". The button is no longer unlocked when the checkbox is changed. I will be glad for any help.

My script:

.directive('submitIfValid', ['$http', '$timeout', '$parse', function ($http, $timeout, $parse) {
    return {
        restrict: 'A',
        require: '^form',
        scope: {
            onSuccess: "=",
            ngModel: "="
        },
        link: function (scope, element, attrs, form) {

            element.on('click', handleClick);

            function handleClick() {
                markFieldsAsDirty();
                submitFormIfValid(this);

                markFieldsAsEmpty();
                scope.$apply();
            }

            function submitFormIfValid(element) {
                if (form.$valid) {
                    var object = {};
                    (new FormData(element.form)).forEach(function (value, key) {
                        object[key] = value;
                    });
                    var json = JSON.stringify(object);
                    form.$loading = true;
                    $http({
                        url: element.form.action,
                        method: element.form.method,
                        data: json
                    }).then(function (result) {
                        console.log(result);
                        form.$loading = false;
                        form.$success = result.data.Result;
                        form.$error = result.data.Error;
                        
                        if (form.$success && scope.onSuccess) {
                            scope.onSuccess();
                        }

                    }).catch(function () {
                        form.$loading = false;
                    });
                }
            }
            function markFieldsAsDirty() {
                Object.keys(form)
                    .filter(function (key) {
                        return key.indexOf('$') === -1;
                    })
                    .forEach(function (fieldName) {
                        return form[fieldName].$setTouched();
                    });
            }
            function markFieldsAsEmpty() {
                form.$setPristine();
                Object.keys(form)
                    .filter(function (key) {
                        return key.indexOf('$') === -1;
                    })
                    .forEach(function (fieldName) {
                        form[fieldName].$setViewValue(undefined);
                        form[fieldName].$setUntouched();
                        return;
                    });
                element[0].form.reset();
            }
        }
    };
}])

My HTML:

<form name="feedbackForm" action="/feedback" method="POST">
    <label class="effect-control">
        <input type="text" name="Name" data-ng-model="model.name" data-ng-required="true">
        <span>Name</span>
    </label>
    <div data-ng-if="feedbackForm.Name.$touched && feedbackForm.Name.$error.required">field required</div>
    
    <label class="effect-control tel-control contact">
        <input type="text" name="Phone" data-ng-model="model.phone" data-ng-required="true" data-ng-pattern="/^[0-9()+-]+$/">
        <span>Phone</span>                        
    </label>
    <div data-ng-if="feedbackForm.Phone.$touched && feedbackForm.Phone.$error.required">field required</div>
    <div data-ng-if="feedbackForm.Phone.$touched && feedbackForm.Phone.$error.pattern">incorrect phone</div>
        
    <label class="checkbox">
        <input type="checkbox" name="Policy" data-ng-model="model.policy" value="true"> I Agree
    </label>
    <button type="button" data-ng-disabled="!model.policy" submit-if-valid>Send</button>
</form>


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source