Reputation: 27962
The title probably doesn't make sense, but basically I;t like to take an object like this...
[
{
menuItemTitle: 'Edit Page',
checkPermissions: function($http, $timeout){
// Do something which uses $http or $timeout
}
}
]
I have a menu directive which needs to loop through the array and for each item it needs to check whether .checkPermissions is a function and if it is a function then it needs to call it and somehow get all the dependencies passed in and allow them to be used inside the function, just like Angular does when you create a controller, factory, service etc.
I guess the function also needs to run in the context in which it was called so that other variables in the original context would also be available on the "this" object, e.g.
var siteId = 'google.com'
var menu = [
{
menuItemTitle: 'Edit Page',
checkPermissions: function($http, $timeout){
if(siteId === 'google.com'){
$http.get(stuff);
}
}
}
]
I can find loads of articles about how to use DI in controllers and services but none about using the injector system yourself to run passed functions like this, so I presume it's really hard? My JS skills don't stretch to understanding the core Angular code so any help would really be appreciated.
Upvotes: 0
Views: 414
Reputation: 222696
It should use the same way as being used in Angular and third-party libraries for DI-enabled functions, $injector.invoke
.
It basically calls checkPermissions
(which should be a function or inline array annotation) with respective dependencies as params, an optional argument can be also provided as this
context for a function.
var checkPermissionsResult = $injector.invoke(checkPermissions, this);
Upvotes: 1