Skip to content

Instantly share code, notes, and snippets.

@sorich87
Last active January 3, 2016 21:39
Show Gist options
  • Save sorich87/8523585 to your computer and use it in GitHub Desktop.
Save sorich87/8523585 to your computer and use it in GitHub Desktop.
// NOT TESTED. Use with `<div my-controller="exampleCtrl" locals="{some: someVal, another: anotherVal}">`
angular.module('exampleApp', [])
.directive('myController', ['$controller', '$q', function ($controller, $q) {
return {
scope: true,
priority: 500,
link: function (scope, elem, attrs) {
var locals = scope.$eval(attrs.locals);
var promises = {};
angular.forEach(locals, function (local, name) {
if (angular.isFunction(local)) {
var value = local();
if (value && value.then) {
promises[name] = value;
}
}
});
angular.extend(locals, {$scope: scope});
if (promises) {
$q.all(promises).then(function (resolves) {
angular.extend(locals, resolves);
$controller(attrs.myController, locals);
});
} else {
$controller(attrs.myController, locals);
}
}
};
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment