0

I have a parent controller and i want to call a function in link from controller but when try to access it gives

TypeError: undefined is not a function

My Controller:

scope.test = function(m)
{
 linkfunction(m);
}

My Directive: .......... link: function(scope, element, attrs) {

 linkfunction = function (n){
  ........somethings........
  }

  ..........
  }

How can i call function in link from directive?

user4773604
  • 451
  • 3
  • 9
  • 16

1 Answers1

0

1) Using Isolated Scope

HTML

<div test-directive callback-fun="testFunction()"></div>

Controller:

$scope.testFunction = function(){
  console.log("test")
}

Directive:

.directive('testDirective', function() {
     return {
         scope: {
            callbackFun: '&'
         },
         link: function(scope, element, attrs) 
         {
            scope.callbackFun();
         }
     }
 })

2) Without using Isolated Scope

HTML:

<div test-directive></div>

Controller:

$scope.testFunction = function(){
  console.log("test")
}

Directive:

.directive('testDirective', function() {
     return {
         link: function(scope, element, attrs) 
         {
            scope.testFunction();
         }
     }
 })
Raj
  • 321
  • 1
  • 4