我目前收到常见的“$digest already in progress”错误消息。
有没有不需要使用 scope.$apply 的解决方案?
谢谢你的帮助!
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>