vincentsty vincentsty - 6 months ago 21
AngularJS Question

$scope and setPristine() not working inside factory

I am trying to write convert my current api call into a factory which is reusable inside

ApiService
factory. However, I can't seem to clear the form.

So, what i can i do to make the clearForm function work as it is.

<form name="formName">
<input type="name" ng-model="form.username"></input>
<input type="password" ng=model="form.password"></input>
<button type="submit" ng-click="submitForm()">Submit</button>
<form>

Answer

form is in $scope. You can't access it without using $scope and factory don't have $scope.

try like this

$scope.formName.$setPristine();

All you can do is to pass that form to service and clear it

'clearForm': function(formName) {
            formName.$setPristine();
            formName.$setUntouched();
        }

from controller

FormFactory.clearForm($scope.formName);
Comments