From b3777f275c6bd2bd4a88963fd03828eb7cf3aca8 Mon Sep 17 00:00:00 2001 From: Lucas Galfasó Date: Fri, 24 May 2013 21:18:51 -0300 Subject: feat(directive): support as instance syntax Support controller: 'MyController as my' syntax for directives which publishes the controller instance to the directive scope. Support controllerAs syntax to define an alias to the controller within the directive scope. --- src/ng/compile.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/ng/compile.js b/src/ng/compile.js index 5b12883d..6aebe537 100644 --- a/src/ng/compile.js +++ b/src/ng/compile.js @@ -961,16 +961,25 @@ function $CompileProvider($provide) { $element: $element, $attrs: attrs, $transclude: boundTranscludeFn - }; + }, controllerInstance; controller = directive.controller; if (controller == '@') { controller = attrs[directive.name]; } + controllerInstance = $controller(controller, locals); $element.data( '$' + directive.name + 'Controller', - $controller(controller, locals)); + controllerInstance); + if (directive.controllerAs) { + if (typeof locals.$scope !== 'object') { + throw new Error('Can not export controller as "' + identifier + '". ' + + 'No scope object provided!'); + } + + locals.$scope[directive.controllerAs] = controllerInstance; + } }); } -- cgit v1.2.3