javascript - AngularJs calling function in link from controller -


i have parent controller , want call function in link controller when try access gives

typeerror: undefined not function 

my controller:

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

my directive: .......... link: function(scope, element, attrs) {

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

how can call function in link directive?

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

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -