You could always create a basic pub / sub type of class that broadcast a
message to any listeners.
var EventTrigger = function() {
var _listeners = [];
var _trigger = function() {
if(_listeners) {
_listeners.forEach(function(listener) {
listener();
});
}
};
var _listen = function(listener) {
_listeners.push(listener);
}
return {
trigger: _trigger,
listen: _listen
};
}
Then you can instantiate an instance on your controller $scope as say:
$scope.SomeEventTrigger = new EventTrigger();
and raise an event:
$scope.SomeEventTrigger.trigger();
Then in your directive you can bind to the instance using the '=' binding
on your isolated scope:
scope: {
EventTrigger: '=eventTrigger'
}
and add a listener to the instance:
$scope.EventTrigger.listen(function() {
$scope.directiveFunctionIWantToCall();
});
Here's a jsfiddle showing a working example:
http://jsfiddle.net/R8Bfu/
On Wednesday, October 31, 2012 4:54:17 PM UTC-4, Olivier Clément wrote:
>
> Hi all, I have a newb question here;
>
> Let's say I have a Directive in which I define a few functions that will
> affect its behavior/presentation,
> but I want to be able to call these functions from outside the directive
> (the page's controller for instance);
>
> What would be the best approach?
>
> Thanks
>
>
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.