Skip to content Skip to sidebar Skip to footer

How To Tell Angular Form Custom Directive Is Invalid

I have custom angular directive:

Solution 1:

You can use the ngModel validators:

.directive("smth", function($rootScope) {
    var link = function(scope, element, attrs, ngModelCtrl) {

        // Add custom validator
        ngModelCtrl.$validators["timeString"] = function(modelValue) {
            return !$rootScope.timeStringValid(modelValue);
        }
    };
    return {
        restrict: "E",
        scope: {
            name: "=name",
            label: "=label",
            ngModel: "=",
            required: "=required"
        },
        // require ngModel controllerrequire: "ngModel",
        link: link,
        templateUrl: "smth.html"
    };
});

This way angular will include the validation errors to its $invalid property and the $errors (myForm.myFieldName.$errors.timeString)

Post a Comment for "How To Tell Angular Form Custom Directive Is Invalid"