Skip to content

Instantly share code, notes, and snippets.

@alanerzhao
Forked from auser/Validationexample.js
Last active August 29, 2015 14:21
Show Gist options
  • Save alanerzhao/745f377ecd3fdcc83e60 to your computer and use it in GitHub Desktop.
Save alanerzhao/745f377ecd3fdcc83e60 to your computer and use it in GitHub Desktop.
var app = angular.module('validationExample', []);
app.controller('signupController', ['$scope', function($scope) {
$scope.submitted = false;
$scope.signupForm = function() {
if ($scope.signup_form.$valid) {
} else {
$scope.signup_form.submitted = true;
}
}
}]);
app.directive('ensureUnique', ['$http', '$timeout', function($http, $timeout) {
var checking = null;
return {
require: 'ngModel',
link: function(scope, ele, attrs, c) {
scope.$watch(attrs.ngModel, function(newVal) {
if (!checking) {
checking = $timeout(function() {
$http({
method: 'POST',
url: '/api/check/' + attrs.ensureUnique,
data: {'field': attrs.ensureUnique}
}).success(function(data, status, headers, cfg) {
c.$setValidity('unique', data.isUnique);
checking = null;
}).error(function(data, status, headers, cfg) {
checking = null;
});
}, 500);
}
});
}
}
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment