Merge branch 'directives' of github.com:angular/angular.js into directives

This commit is contained in:
Misko Hevery 2010-04-07 14:15:09 -07:00
commit 3d0b40fee2
4 changed files with 42 additions and 2 deletions

View file

@ -15,7 +15,7 @@
<tr><th colspan="3">Input text field</th></tr>
<tr>
<td>basic</td>
<td><input type="text" name="text.basic" ng-required /></td>
<td><input type="text" name="text.basic" ng-required ng-validate="number" ng-format="number"/></td>
<td>text.basic={{text.basic}}</td>
</tr>
<tr>

View file

@ -87,3 +87,18 @@ angularService("$hover", function(browser) {
}
});
}, {inject:['$browser']});
angularService("$invalidWidgets", function(){
var invalidWidgets = [];
invalidWidgets.markValid = function(element){
var index = indexOf(invalidWidgets, element);
if (index != -1)
invalidWidgets.splice(index, 1);
};
invalidWidgets.markInvalid = function(element){
var index = indexOf(invalidWidgets, element);
if (index === -1)
invalidWidgets.push(element);
};
return invalidWidgets;
});

View file

@ -22,7 +22,8 @@ function valueAccessor(scope, element) {
var validatorName = element.attr('ng-validate') || NOOP,
validator = compileValidator(validatorName),
required = element.attr('ng-required'),
lastError;
lastError,
invalidWidgets = scope.$invalidWidgets || {markValid:noop, markInvalid:noop};
required = required || required === '';
if (!validator) throw "Validator named '" + validatorName + "' not found.";
function validate(value) {
@ -30,6 +31,10 @@ function valueAccessor(scope, element) {
if (error !== lastError) {
elementError(element, NG_VALIDATION_ERROR, error);
lastError = error;
if (error)
invalidWidgets.markInvalid(element);
else
invalidWidgets.markValid(element);
}
return value;
}

View file

@ -54,3 +54,23 @@ describe("services", function(){
});
});
describe("service $invalidWidgets", function(){
var scope;
beforeEach(function(){
scope = null;
});
afterEach(function(){
if (scope && scope.$element)
scope.$element.remove();
});
it("should count number of invalid widgets", function(){
var scope = compile('<input name="price" ng-required></input>').$init();
expect(scope.$invalidWidgets.length).toEqual(1);
scope.price = 123;
scope.$eval();
expect(scope.$invalidWidgets.length).toEqual(0);
scope.$element.remove();
});
});