AngularJS: Empty - latest CI build
by vojtajina
HTML
<script src="http://ci.angularjs.org/job/angular.js-vojta/238/artifact/build/pkg/1.0.0rc2-ad82777c/angular-1.0.0rc2-ad82777c.js"></script>
<div ng-app="my">
<form name="myForm" ng-controller="Ctrl">
<div ng-repeat="item in items">
<input ng-model="item.name" type="text" not-in="namesFrom(items, $index)" required >
</div>
<p>I would like the form to be invalid if any pair of these items have the same name.</p>
<button ng-click="add()">Add</button>
</form>
{{myForm}}
</div>
CSS
input.ng-invalid.ng-dirty {
background-color: #FA787E;
}
JavaScript
function Ctrl($scope) {
$scope.add = function() {
$scope.items.push({name: ""});
};
$scope.items = [{name: 'One'},{name:'Two'}];
$scope.namesFrom = function(items, ignoreIdx) {
var names = [];
items.forEach(function(item, idx) {
if (ignoreIdx !== idx && item.name) names.push(item.name);
});
return names;
};
}
angular.module('my', []).directive('notIn', ['$parse', function($parse) {
return {
require: 'ngModel',
link: function(scope, elm, attrs, ctrl) {
var listExp = $parse(attrs.notIn);
ctrl.$parsers.push(function(value) {
if (listExp(scope).indexOf(value) === -1) {
ctrl.$setValidity('unique', true);
return value;
} else {
ctrl.$setValidity('unique', false);
return undefined;
}
});
}
};
}]);