0

how can I pass function to angularjs directive and execute it with parameters from the directive?

I am trying to render my directive with this "test-directive(cb="getDetails()") and it doesn't work

1 Answer 1

1

Say a directive is like this

angular.module('app', []).directive('testDirective', function () {
    return {
        restrict: 'E',
        scope: {
            callback: '&'
        }
    };
});

Then in html where you integrate the directive set the callback to a function which will be defined on your controller.

<test-directive callback="getDetails(newValue, oldValue)"/>

And in controller define getDetails

$scope.getDetails = function (newValue, oldValue) {
    // do whatever you want here
}

While you invoke your callback from directive then you have to call it like

$scope.callback({
    newValue: <a value>,
    oldValue: <another value>
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.