0
votes

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?

1
Show proper code not ........somethings........Satpal
Is that linkfunction in a other controller?hurricane
it is in directive link,not in a other controller,there is only one controlleruser4773604
If you have to do something like that you are doing something wrong. Expose both methods to the view and when you use ng-click or w/e call both methods there.Adrian Neatu
This might be something that you need stackoverflow.com/questions/16839259/…BKM

1 Answers

0
votes

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();
         }
     }
 })