I am currently facing the common error message which says "$digest already in progress."
Is there a workaround that does not involve using scope.$apply?
Any assistance would be greatly appreciated. Thank you!
directive('ngUsername', ['$http', function($http) {
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, elm, attr, ctrl) {
//when the scope changes, check the email.
scope.$watch(attr.ngModel, function(value) {
$http.get('http://tagsy.co/api/username?token=' + apiToken + '&username=' + value).success(function(data) {
scope.$apply(function(s) {
ctrl.$setValidity('uniqueUsername', data.unique);
});
});
});
}
}
}]);
<input type="text" name="username" ng-model="username" placeholder="Username" ng-minlength="3" ng-maxlength="15" ng-pattern="/^[A-Za-z0-9]+$/" autocomplete="off" ng-username required/>
<div ng-show="registerForm.username.$dirty && registerForm.username.$invalid">
<span class="error" ng-show="registerForm.username.$error.required">Please choose a username.</span>
<span class="error" ng-show="registerForm.username.$error.minlength">Username is too short.</span>
<span class="error" ng-show="registerForm.username.$error.maxlength">Username is too long.</span>
<span class="error" ng-show="registerForm.username.$error.pattern">Username can only be letters and numbers.</span>
<span class="error" ng-show="registerForm.username.$error.uniqueUsername">Username is already taken.</span>
</div>