Created
October 15, 2015 21:55
-
-
Save ekazakov/770fe84bbd3b6e68bdee to your computer and use it in GitHub Desktop.
Pre resolve promises
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
angular.module('app', []) | |
.decorator('$controller', function ($delegate) { | |
return function () { | |
var result = $delegate.apply(null, arguments); | |
return result; | |
}; | |
}) | |
.factory('lib',function ($q) { | |
return $q.when('Hello'); | |
}) | |
.controller('Foo', resolveDeps(/*@ngInject*/ function Foo (lib) { | |
this.x = 2; | |
this.msg = lib; | |
})); | |
function resolveDeps (ctrl) { | |
const deps = angular.injector().annotate(ctrl); | |
return ['$q'].concat(deps, function ($q) { | |
var args = Array.prototype.slice.call(arguments, 1); | |
$q.all(args).then(function(results) { | |
ctrl.slice(-1)[0].apply(this, results); | |
}.bind(this)); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment