I am looking to invoke a function that is defined in a directive, which is the opposite of what was discussed in this particular StackOverflow post
Although I have attempted this approach, it does not seem to work.
app.directive('myDirective', function() {
return {
link: function(scope, element, attrs) {
scope.someDirectiveFn = function(arg) {
return "in directive";
};
},
}
});
function MyCtrl($scope) {
alert($scope.someDirectiveFn());
}
Is it possible to achieve this? If so, how can it be done? Or is it considered bad practice?
EDIT
I found an alternative solution:
.controller('MyCtrl', function($scope) {
alert($scope.func());
})
.directive('myDirective', function() {
return {
controller: function($scope, $element){
$scope.func = function() {
return "text";
};
}
}
});