Angular where one of two attributes is required

How do I write a directive that requires either ng-model

or k-ng-model

? The documentation does not cover this :(

app.directive('myDir', function () {
    return {
        require: 'ngModel || kNgModel',
        // omitted
    };
});

      

+3


source to share


1 answer


You need to pass them as an array of strings.

You cannot tell Angular that at least one of these requirements should be available, so set them as optional and check the communication function if one is available. Update your code to:



app.directive('myDir', function () {
    return {
        require: ['?ngModel', '?kNgModel'],
        link: function(scope, element, attrs, controllers){
            if(!controllers[0] && !controllers[1]){
                throw 'myDir expects either ng-model or k-ng-model to be defined';
            }
        }
    };
});

      

+3


source







All Articles