AngularJS How to call directive function from controller

Monojit Sarkar

i was looking for a way to know how to call function inside directive from controller. i got the snip but hence i am new in angular that is why the below code flow is not very clear. anyone mind to explain how the code is working. thanks

// Directive 
<map set-fn="setDirectiveFn(theDirFn)"></map>

scope: { setFn: '&' },
link: function(scope, element, attrs) {
    scope.updateMap = function() {
       alert('inside updateMap()');
    }
    scope.setFn({theDirFn: scope.updateMap});
}

// Controller
<button ng-click="directiveFn()">call directive function</button>
function MyCtrl($scope) {
    $scope.setDirectiveFn = function(directiveFn) {
        $scope.directiveFn = directiveFn;
    };
}
Lex

Starting with the controller, this block creates a setDirectiveFn() method on the $scope object in your controller that takes a single parameter (directiveFn) and then uses that parameter to create a directiveFn() method on the $scope object in your controller.

    $scope.setDirectiveFn = function(directiveFn) {
        $scope.directiveFn = directiveFn;
    };

Inside the directive it is creating an updateMap() method on the scope object in the directive and then calling the setFn() method which is mapped to the $scope.setDirectiveFn() method by this line: <map set-fn="setDirectiveFn(theDirFn)"></map> in your HTML and this line: scope: { setFn: '&' } in your directive. It is passing the scope.updateMap() method which effectively sets $scope.directiveFn() in your controller equal to scope.updateMap() in your directive.

link: function(scope, element, attrs) {
    scope.updateMap = function() {
       alert('inside updateMap()');
    }
    scope.setFn({theDirFn: scope.updateMap});
}

The button is then calling $scope.directiveFn() in your controller which has been mapped to scope.updateMap() in your directive.

<button ng-click="directiveFn()">call directive function</button>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to call one directive controller function from another one directive using angularjs

From Dev

angularjs directive will not call controller function

From Dev

Angularjs directive call controller function

From Dev

Angularjs directive call controller function

From Dev

How to call a directive function from controller?

From Dev

Angularjs pass function from Controller to Directive (or call controller function from directive) - with parameters

From Java

Call a controller function from a directive without isolated scope in AngularJS

From Dev

how to call parent function controller from directive react component angularjs + reactjs?

From Dev

How to pass data from controller to directive from ajax call AngularJS

From Dev

How to call a function from another controller in angularjs?

From Dev

how to call controller function from template in angularjs

From Dev

How to call a function from a directive from an outside controller?

From Dev

How to call angular controller scope function from directive with parameters?

From Dev

How to call angular controller scope function from directive with parameters?

From Dev

AngularJS Trigger directive from controller call

From Dev

How to pass a function handler from controller into directive isolated scope in AngularJs?

From Dev

How to pass a function handler from controller into directive isolated scope in AngularJs?

From Dev

How to pass a value from an AngularJS directive to a function in parent controller

From Dev

AngularJS Controller - Directive -> invokation of Controller function from directive

From Dev

How to call a directive from an another directive in AngularJS?

From Dev

How to call controller function from other controller angularjs

From Dev

How to call a function in a controller from a different controller AngularJS

From Dev

Call Angular controller function from directive

From Dev

Call controller function from angular directive

From Dev

Call controller function from directive with arguments

From Dev

Call function in controller from a nested directive

From Dev

Call controller function from directive with arguments

From Dev

AngularJS: Exeucting function from controller in a directive

From Dev

AngularJS: Exeucting function from controller in a directive

Related Related

  1. 1

    how to call one directive controller function from another one directive using angularjs

  2. 2

    angularjs directive will not call controller function

  3. 3

    Angularjs directive call controller function

  4. 4

    Angularjs directive call controller function

  5. 5

    How to call a directive function from controller?

  6. 6

    Angularjs pass function from Controller to Directive (or call controller function from directive) - with parameters

  7. 7

    Call a controller function from a directive without isolated scope in AngularJS

  8. 8

    how to call parent function controller from directive react component angularjs + reactjs?

  9. 9

    How to pass data from controller to directive from ajax call AngularJS

  10. 10

    How to call a function from another controller in angularjs?

  11. 11

    how to call controller function from template in angularjs

  12. 12

    How to call a function from a directive from an outside controller?

  13. 13

    How to call angular controller scope function from directive with parameters?

  14. 14

    How to call angular controller scope function from directive with parameters?

  15. 15

    AngularJS Trigger directive from controller call

  16. 16

    How to pass a function handler from controller into directive isolated scope in AngularJs?

  17. 17

    How to pass a function handler from controller into directive isolated scope in AngularJs?

  18. 18

    How to pass a value from an AngularJS directive to a function in parent controller

  19. 19

    AngularJS Controller - Directive -> invokation of Controller function from directive

  20. 20

    How to call a directive from an another directive in AngularJS?

  21. 21

    How to call controller function from other controller angularjs

  22. 22

    How to call a function in a controller from a different controller AngularJS

  23. 23

    Call Angular controller function from directive

  24. 24

    Call controller function from angular directive

  25. 25

    Call controller function from directive with arguments

  26. 26

    Call function in controller from a nested directive

  27. 27

    Call controller function from directive with arguments

  28. 28

    AngularJS: Exeucting function from controller in a directive

  29. 29

    AngularJS: Exeucting function from controller in a directive

HotTag

Archive