it seems that there are some questions online with similar matters but no solution worked for me.
I'm having a directive, let's say that includes a function like this (within the directive):
link: function(scope, element, attrs) {
scope.myfunction = function (){
console.log('function run');
};
}
Witch i want to make it run (call it) from a controller with something simple like this:
$scope.myFunction();
So i can call it from a part of my page outside the directive code let's say like this:
<button ng-click="myFunction()">Run</button>
Is it possible?
Finally i got the solution by broadcasting scope like this.
In controller:
$scope.startFunction = function(){
$scope.$broadcast('startfunction');
};
In directive:
scope.$on('startfunction', function () {
console.log('function run');
});
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments