AlfaTeK
AlfaTeK

Reputation: 7765

How to pass multiple functions to an attribute directive in Angular?

I have an attribute directive that I use to send the value of an input field to my server (my-auto-save), and that works fine. Now I want to add to that directive the capability of validating my value and only in case of success it sends the value to the server.

For this I thought about adding another attribute to my tag like so:

<input type="text" my-auto-save="saveHandler(field, value)" my-auto-save-validations="validateNumeric(value)" />

My auto save directive is like this:

myMod.directive("myAutoSave",
    function () {
        return {
            restrict: "A",
            scope: {
                saveHandler: "&myAutoSave"
            },
            require: "ngModel",
            link: function (scope, elm, attr) {
                var fieldName = "test";
                var newValue = "new value test";
                scope.saveHandler({fieldChanged: fieldName, newValue: newValue});
            }
        };
    }
);

Now, before calling the scope.saveHandler how can I call my validation function passing the newValue value to it?

I know how to access it like attr.myAutoSaveValidations but I don't know how to correctly call the function passed like that...

Upvotes: 0

Views: 740

Answers (1)

Joao Leal
Joao Leal

Reputation: 5542

You can add more stuff to your scope:

scope: {
   saveHandler: "&myAutoSave",
   validationFunction: "&myAutoSaveValidations"
},

Upvotes: 1

Related Questions