Peter Van de Laar
Peter Van de Laar

Reputation: 399

angular calling controller function in from directive controller

I have a question that should be really simple, but i cannot find the answer.

I have controller a:

app.controller('myController', function ($scope){

    $scope.MyParentFunction = function(){ 

              alert("it does not work..");

    };     

}

And Controller b, which is inside a directive:

app.directive('myDirective', function () {

var directive = {};

    //properties
    directive.scope = {
        date: '=ngModel',
    },

    //controller
    directive.controller = function ($scope) {

        $scope.CallParentFunction = function(){

          //Call Function From Parent scope here
          $scope.MyParentFunction();     

        }

    },

    //call parent function in template
    directive.template = '<div ng-click="CallParentFunction()">Call</div>'; 

    return directive;

});

How can i call "MyParentFunction()" from my directive controller? Thanks in advance!

Upvotes: 0

Views: 944

Answers (1)

Mohamed dev
Mohamed dev

Reputation: 172

Hope this will help:

All you need is to bind your parent function

directive.scope = {
    date: '=ngModel',
    callParentFunction: "&"  //function binding 
}

http://plnkr.co/edit/y7I1g7bKok8NOADipqX4?p=preview

Take a look at the angularjs documentation

https://docs.angularjs.org/guide/directive

Upvotes: 1

Related Questions