UNPKG

1.86 kBJavaScriptView Raw
1/**
2 * @name Compiler Ng
3 */
4import angular from 'angular';
5
6const angularModule = angular.module('Ring.compiler', []).
7 factory('rgCompiler', function rgCompilerFactory($q, $controller, $injector, $compile) {
8 return options => {
9 const template = options.template;
10 const controller = options.controller;
11 const controllerAs = options.controllerAs;
12 const resolve = angular.extend({}, options.resolve);
13 const bindToController = options.bindToController;
14
15 angular.forEach(resolve, (value, key) => {
16 if (angular.isString(value)) {
17 resolve[key] = $injector.get(value);
18 } else {
19 // Use comma expression to disable babel-plugin-angular-annotate
20 // Otherwise is fails with "Maximum call stack size exceeded" error
21 resolve[key] = $injector.invoke((0, value));
22 }
23 });
24
25 angular.extend(resolve, options.locals);
26
27 return $q.all(resolve).then(locals => {
28 const element = options.element || angular.element('<div>').
29 html(template.trim()).
30 contents();
31 const linkFn = $compile(element, locals.$transclude);
32 locals.$element = element;
33
34 return {
35 locals,
36 element,
37 link: function link(scope) {
38 locals.$scope = scope;
39
40 if (controller) {
41 const invokeCtrl = $controller(controller, locals, true);
42
43 if (bindToController) {
44 angular.extend(invokeCtrl.instance, locals);
45 }
46
47 const ctrl = invokeCtrl();
48
49 element.data('$ngControllerController', ctrl);
50
51 if (controllerAs) {
52 scope[controllerAs] = ctrl;
53 }
54 }
55
56 return linkFn(scope);
57 }
58 };
59 });
60 };
61 });
62
63export default angularModule.name;