1

I got a problem by passing a function to a directive ( familiar to this post: AngularJS - pass function to directive but i can´t get it working)

Here is my Code:

Directive:

.directive('testdirective', function(){
  return{
    restrict: 'E',
    scope: {
      onClick: '&'
    },
    controller: 'TestController',
    controllerAs: 'tc',
    bindToController: true,
    template: '<div><button ng-click="onClick()">What UP</button></div>',
    replace: true
  }
})

Controller:

  TestController.$inject = ["$scope"];
  function TestController($scope) {
    $scope.testFunction = function(){
      alert("I´m the alert of the TestContoller");
    };
    $scope.test = 'test';
  }

HTML:

<div>
  <testdirective on-click="testFunction()"></testdirective>
</div>

What I want sounds very simple, I just want to pass the function to the directive and execute it with the ng-click on the button.

For me my code looks exactly like this fiddle but mine is not working :/

Would be awesome if someone got some hints for me.

EDIT

My directive will need his own controller !

Later the function to be passed in will come from another controller !!!

4
  • What is the error that you get? Commented Jan 25, 2016 at 11:11
  • nothing - just not executed Commented Jan 25, 2016 at 12:56
  • The working JSFiddle has the controller outside the directive. Your code has the controller inside the directive. Your directive uses an isolate scope. Functions on the scope outside will not be inherited by the scope inside. Commented Jan 25, 2016 at 13:01
  • But I need a own controller for my directive. Later the function to be passed in will come from another controller !!! For Example I got a TestController2 with a function and this function i´ll pass to my directive and execute by ng-click Commented Jan 25, 2016 at 13:04

4 Answers 4

2

The fiddle is not the same as your code.

You have set the controller of your directive to be "TestController". I assume what you wanted to do was:

.directive('testdirective', function(){
    return{
        restrict: 'E',
        scope: {
            onClick: '&'
        },
        template: '<div><button ng-click="onClick()">What UP</button></div>',
        replace: true
    }
});

and in your HTML,

<div ng-controller="TestController">
  <testdirective on-click="testFunction()"></testdirective>
</div>

EDIT: Based on OP's comment

app.directive('testdirective', function(){
        return{
            restrict: 'E',
            scope: {
                onClick: '&'
            },
            template: '<div><button ng-click="tc.onClick()">What UP</button></div>',
            replace: true,
            controller: 'TestController',
            controllerAs: 'tc',
            bindToController: true
        }
    });

    app.controller('TestController', function ($scope) {
        console.log($scope);
    }) ;

    app.controller('AnotherController', function ($scope) {
        $scope.testFunction = function(){
            alert("I´m the alert of the TestContoller");
        };

        $scope.test = 'test';
    });

And, your HTML

<div ng-controller="AnotherController">
    <testdirective on-click="testFunction()"></testdirective>
</div>

You are telling the directive to bindToController. So within the directive's template, onClick is bound to the controller and not the scope. So, you access the onclick via the controller as tc.onClick() in the directive's template.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks that much. The problem is, that I need my own controller to calculate some data (later)---> so i need: controller: 'TestController', controllerAs: 'tc', bindToController: true, Later the function should be passed in from another Controller
0

You may want to pass a method as a reference:

1.Pass the function as a reference and not a call:

<div>
  <testdirective on-click="testFunction"></testdirective>
</div>

2.Update the directive:

.directive('testdirective', function(){
    return{
        restrict: 'E',
        scope: {
            onClick: '='
        },
        template: '<div><button ng-click="onClick()">What UP</button></div>',
        replace: true
    }
});

JSFIDDLE.

1 Comment

Thanks that much. The problem is, that I need my own controller to calculate some data (later)---> so i need: controller: 'TestController', controllerAs: 'tc', bindToController: true,
0

Well, in your testdirective,you defined controller TestController. The testFunction() that you try to calling via onClick directive scope parameter is defined in controller TestController which is directive controller. So, rather than calling via onClick you can call directly like

template: '<div><button ng-click="testFunction()">What UP</button></div>'.

Its very confusing ,you defining controller in directive and again referring it's one function via same directive's scope parameter which look like recursive.

If you want to call via directive scope parameter then you should do belowe changes.

for e.g. JS :

<div ng-controller="TestController" ng-app="dr">
  <testdirective on-click="testFunction()"></testdirective>
</div>

 app.directive('testdirective', function() {
    return{
    restrict: 'E',
    scope: {
      onClick: '&'
    }, 
    template: '<div><button ng-click="onClick()">What UP</button></div>',
    replace: true
  }
});

1 Comment

Thanks that much. The problem is, that I need my own controller to calculate some data (later)---> so i need: controller: 'TestController', controllerAs: 'tc', bindToController: true, Later the function should be passed in from another Controller
0

Directivie:

.directive('testdirective', function(){
return{
restrict: 'E',
scope: {
  onClick: '=onClick'
},
controller: 'TestController',
controllerAs: 'tc',
bindToController: true,
template: '<div><button ng-click="onClick()">What UP</button></div>',
replace: true
}
})

use '=' instead of '&' so you can fetch the html function in your directive. and you can simply pass onClick parameter through HTML HTML:

<div>
<testdirective on-click="testFunction()"></testdirective>
</div>

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.