Last active
January 3, 2016 21:39
-
-
Save sorich87/8523585 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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