aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLucas Galfasó2013-05-24 21:18:51 -0300
committerMisko Hevery2013-07-31 10:30:58 -0700
commitb3777f275c6bd2bd4a88963fd03828eb7cf3aca8 (patch)
tree86d62c51db20647a59bb76912d8122cfcc0b7fe0 /src
parentaa5a16224bb4e19f44fafebaf04ece7665d5ad5b (diff)
downloadangular.js-b3777f275c6bd2bd4a88963fd03828eb7cf3aca8.tar.bz2
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.
Diffstat (limited to 'src')
-rw-r--r--src/ng/compile.js13
1 files changed, 11 insertions, 2 deletions
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;
+ }
});
}