To separate the view from the business logic, you must use custom validators.
If you used ngForm and ngModel for an input field, for example:
<div ng-controller="InputController as inputCtrl"> <ng-form name="testForm"> <input type="text" name="testModel" ng-model="testModel"/> </ng-form> </div>
You can use the following controller:
app.controller('InputController', ['$scope', function($scope) { var validator = function(modelValue, viewValue) { var isRange1To5 = modelValue >= 1 && modelValue <= 5; var isRange10To15 = modelValue >= 10 && modelValue <= 15; return isRange1To5 || isRange10To15; }; $scope.testForm.testModel.$validators['rangeValidator'] = validator; }])
The min / max directives (and others) do just that. They add a validator to the model - which runs every time the input changes.
See the Angular docs for the issue for more information: https://docs.angularjs.org/guide/forms#custom-validation
source share