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

Andrey

I have the following function in controller:

  angular.module('app').controller('BodyController', function(){
    this.click = function(message){
      alert(message);
    }
  })

I want to pass this function into directive's isolated scope to call it with some specific params, something like that:

  angular.module('app').directive('custom', function(){
    return {
      restrict: 'A',
      scope: {
        text: '@',
        click: '&click'
      },
      link: function(scope, element){
        //...
        scope.click('Hello, Plunker!');
        //...
      }
    }
  })

And I pass thefunction in this way:

<h1 custom text="Hello Plunker!" click="ctrl.click"></h1>

Here is an example: http://plnkr.co/edit/4zkxuHJIB3D339h2Oy60?p=preview

The function is not called. What am I missing?

Thanks in advance

Naeem Shaikh

you can do something like

 angular.module('app').directive('custom', function(){
    return {
      restrict: 'A',
      scope: {
        text: '@',
        click: '=click'
      },
      link: function(scope, element){
      //  console.dir(scope.click);
      element.bind("click",function(){

         scope.click('Hello, Plunker!');
      });

        element.text('Hello, Plunker!');
      }
    }
  })

here you get click function from parent scope by click: '=click'

and bind that function to click on the element.

here is plnkr http://plnkr.co/edit/Ekw8I6Kg6tDOnIrnv4za?p=preview

or you can just pass the parameter into your directive like this http://plnkr.co/edit/mBbZil1jc4El2Jk79ru7?p=preview

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 pass a function handler from controller into directive isolated scope in AngularJs?

From Java

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

From Dev

How to pass an object from a nested directive with isolated scope to parent controller scope in angular

From Dev

AngularJS : Passing a function to an isolated scope of a directive to be called within its controller?

From Dev

Receiving broadcasts and calling controller functions from isolated scope/directive in AngularJS?

From Dev

AngularJS : How to change scope value from isolated scope within directive

From Dev

Calling function of controller from directive when using isolated scope?

From Dev

Pass controller method to directive with isolated scope

From Dev

How to pass object from controller to directive without using $scope in angularJS

From Dev

Pass variable to AngularJS directive without isolated scope

From Dev

AngularJS directive Passing parameters to isolated scope function

From Dev

AngularJS directive Passing parameters to isolated scope function

From Dev

AngularJs directive compile function and isolated scope

From Dev

angularjs directive isolated scope

From Dev

AngularJS Isolated Scope Directive

From Dev

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

From Java

How to Unit Test Isolated Scope Directive in AngularJS

From Dev

AngularJS : Child directive scope to inherit from parent directive isolated scope?

From Dev

Angularjs how to pass data from a controller to a directive

From Dev

Pass value from child directive isolated scope to parent directive isolated scope and trigger watch of parent

From Dev

AngularJS pass URL in attribute to isolated scope of directive - unexpected token ':'

From Dev

Angularjs TreeView Directive With Isolated Scope

From Dev

AngularJS : Directive Isolated Scope Undefined

From Dev

AngularJS overwrites isolated directive scope

From Dev

AngularJS directive: Updating the isolated scope

From Dev

AngularJS Directive Isolated Scope Issue

From Dev

Passing object from parent controller to isolated scope child directive

From Dev

Passing isolated scope variable from directive to its controller

From Dev

Inherited isolated scope from controller to make a reusable directive

Related Related

  1. 1

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

  2. 2

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

  3. 3

    How to pass an object from a nested directive with isolated scope to parent controller scope in angular

  4. 4

    AngularJS : Passing a function to an isolated scope of a directive to be called within its controller?

  5. 5

    Receiving broadcasts and calling controller functions from isolated scope/directive in AngularJS?

  6. 6

    AngularJS : How to change scope value from isolated scope within directive

  7. 7

    Calling function of controller from directive when using isolated scope?

  8. 8

    Pass controller method to directive with isolated scope

  9. 9

    How to pass object from controller to directive without using $scope in angularJS

  10. 10

    Pass variable to AngularJS directive without isolated scope

  11. 11

    AngularJS directive Passing parameters to isolated scope function

  12. 12

    AngularJS directive Passing parameters to isolated scope function

  13. 13

    AngularJs directive compile function and isolated scope

  14. 14

    angularjs directive isolated scope

  15. 15

    AngularJS Isolated Scope Directive

  16. 16

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

  17. 17

    How to Unit Test Isolated Scope Directive in AngularJS

  18. 18

    AngularJS : Child directive scope to inherit from parent directive isolated scope?

  19. 19

    Angularjs how to pass data from a controller to a directive

  20. 20

    Pass value from child directive isolated scope to parent directive isolated scope and trigger watch of parent

  21. 21

    AngularJS pass URL in attribute to isolated scope of directive - unexpected token ':'

  22. 22

    Angularjs TreeView Directive With Isolated Scope

  23. 23

    AngularJS : Directive Isolated Scope Undefined

  24. 24

    AngularJS overwrites isolated directive scope

  25. 25

    AngularJS directive: Updating the isolated scope

  26. 26

    AngularJS Directive Isolated Scope Issue

  27. 27

    Passing object from parent controller to isolated scope child directive

  28. 28

    Passing isolated scope variable from directive to its controller

  29. 29

    Inherited isolated scope from controller to make a reusable directive

HotTag

Archive