AngularJS Link function not called due to attribute name normalization

后端 未结 1 1384
太阳男子
太阳男子 2021-01-27 09:31

I\'ve defined a validator by following the documentation under Custom Validation at https://docs.angularjs.org/guide/forms. But for some reason the link function i

相关标签:
1条回答
  • 2021-01-27 10:17

    I got it to work by naming it has-headers in the HTML and hasHeaders in the JavaScript:

    HTML

    <textarea name="topic1Data" ng-model="inputCtrl.inputValues.topic1Data" rows="10" cols="30" required has-headers></textarea>
    

    JavaScript

    inputForm.directive('hasHeaders', function() {
        return {
            require: 'ngModel',
            link: function(scope, elm, attrs, ctrl) {
                console.log("Evaluating hasAtLeastAHeaderRow validator");
                ctrl.$validators.integer = function(modelValue, viewValue) {
                    if (ctrl.$isEmpty(modelValue)) {
                        // consider empty models to be valid
                        return true;
                    }
    
                    if (INTEGER_REGEXP.test(viewValue)) {
                        // it is valid
                        return true;
                    }
    
                    // it is invalid
                    return false;
                };
            }
        };
    });
    

    Here, I think, is the relevant documentation about AngularJS custom directive naming conventions:

    Normalization

    Angular normalizes an element's tag and attribute name to determine which elements match which directives. We typically refer to directives by their case-sensitive camelCase normalized name (e.g. ngModel). However, since HTML is case-insensitive, we refer to directives in the DOM by lower-case forms, typically using dash-delimited attributes on DOM elements (e.g. ng-model).

    Source: "Normalization"

    0 讨论(0)
提交回复
热议问题