0

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!

2

1 Answer 1

1

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

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

1 Comment

Yep, does the job pretty much! thanks. I knew it would be simple.

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.