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

Vassilis Pits

it seems that there are some questions online with similar matters but no solution worked for me.

I'm having a directive, let's say that includes a function like this (within the directive):

link: function(scope, element, attrs) {

     scope.myfunction = function (){
          console.log('function run');
     };
}

Witch i want to make it run (call it) from a controller with something simple like this:

$scope.myFunction();

So i can call it from a part of my page outside the directive code let's say like this:

<button ng-click="myFunction()">Run</button>

Is it possible?

Vassilis Pits

Finally i got the solution by broadcasting scope like this.

In controller:

$scope.startFunction = function(){
    $scope.$broadcast('startfunction');
};

In directive:

scope.$on('startfunction', function () {
     console.log('function run');
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

AngularJS How to call directive function from controller

From Dev

How to call a directive function from controller?

From Dev

Call controller function from outside

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

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

Call Controller Function from Directive (with params passed from directive)

From Dev

Call Controller Function from Directive (with params passed from directive)

From Dev

AngularJS : Call controller function from outside with vm

From Dev

How to call a custom directive from angular controller

From Dev

Angular JS - Call a Directive function in an outside Controller

From Dev

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

From Dev

Angular: Call directive controller function from separate controller

From Dev

AngularJS call common controller function from outside controller

From Dev

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

From Dev

How to call a function from an Angular directive

From Dev

How to call a controller function from another controller?

From Dev

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

From Java

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

From Dev

Function within angular directive that I can call from the controller

From Dev

how to call specific function from outside an application

From Dev

Angular JS: How to call a function inside link from controller in angular directive

From Dev

How to call scope methods defined in the link function from a directive's controller

From Dev

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

From Dev

Angular JS: How to call a function inside link from controller in angular directive

Related Related

  1. 1

    AngularJS How to call directive function from controller

  2. 2

    How to call a directive function from controller?

  3. 3

    Call controller function from outside

  4. 4

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

  5. 5

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

  6. 6

    Call Angular controller function from directive

  7. 7

    Call controller function from angular directive

  8. 8

    Call controller function from directive with arguments

  9. 9

    Call function in controller from a nested directive

  10. 10

    Call controller function from directive with arguments

  11. 11

    Call Controller Function from Directive (with params passed from directive)

  12. 12

    Call Controller Function from Directive (with params passed from directive)

  13. 13

    AngularJS : Call controller function from outside with vm

  14. 14

    How to call a custom directive from angular controller

  15. 15

    Angular JS - Call a Directive function in an outside Controller

  16. 16

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

  17. 17

    Angular: Call directive controller function from separate controller

  18. 18

    AngularJS call common controller function from outside controller

  19. 19

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

  20. 20

    How to call a function from an Angular directive

  21. 21

    How to call a controller function from another controller?

  22. 22

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

  23. 23

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

  24. 24

    Function within angular directive that I can call from the controller

  25. 25

    how to call specific function from outside an application

  26. 26

    Angular JS: How to call a function inside link from controller in angular directive

  27. 27

    How to call scope methods defined in the link function from a directive's controller

  28. 28

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

  29. 29

    Angular JS: How to call a function inside link from controller in angular directive

HotTag

Archive