aboutsummaryrefslogtreecommitdiffstats
path: root/src/ng/directive/ngView.js
diff options
context:
space:
mode:
authorMisko Hevery2012-06-06 15:54:40 -0700
committerMisko Hevery2013-04-22 23:28:41 -0700
commitcd38cbf975b501d846e6149d1d993972a1af0053 (patch)
tree3857a2879f782c6b7f1350fcd5c8d52e082fd160 /src/ng/directive/ngView.js
parent021bdf3922b6525bd117e59fb4945b30a5a55341 (diff)
downloadangular.js-cd38cbf975b501d846e6149d1d993972a1af0053.tar.bz2
feat(controller): support as instance syntax
Support ng-controller="MyController as my" syntax which publishes the controller instance to the current scope. Also supports exporting a controller defined with route: ````javascript angular.module('routes', [], function($routeProvider) { $routeProvider.when('/home', {controller: 'Ctrl as home', templateUrl: '...'}); }); ````
Diffstat (limited to 'src/ng/directive/ngView.js')
-rw-r--r--src/ng/directive/ngView.js51
1 files changed, 28 insertions, 23 deletions
diff --git a/src/ng/directive/ngView.js b/src/ng/directive/ngView.js
index 5b6d938b..8d7c87c5 100644
--- a/src/ng/directive/ngView.js
+++ b/src/ng/directive/ngView.js
@@ -23,7 +23,7 @@
* @example
<example module="ngView" animations="true">
<file name="index.html">
- <div ng-controller="MainCntl">
+ <div ng-controller="MainCntl as main">
Choose:
<a href="Book/Moby">Moby</a> |
<a href="Book/Moby/ch/1">Moby: Ch1</a> |
@@ -37,26 +37,26 @@
ng-animate="{enter: 'example-enter', leave: 'example-leave'}"></div>
<hr />
- <pre>$location.path() = {{$location.path()}}</pre>
- <pre>$route.current.templateUrl = {{$route.current.templateUrl}}</pre>
- <pre>$route.current.params = {{$route.current.params}}</pre>
- <pre>$route.current.scope.name = {{$route.current.scope.name}}</pre>
- <pre>$routeParams = {{$routeParams}}</pre>
+ <pre>$location.path() = {{main.$location.path()}}</pre>
+ <pre>$route.current.templateUrl = {{main.$route.current.templateUrl}}</pre>
+ <pre>$route.current.params = {{main.$route.current.params}}</pre>
+ <pre>$route.current.scope.name = {{main.$route.current.scope.name}}</pre>
+ <pre>$routeParams = {{main.$routeParams}}</pre>
</div>
</file>
<file name="book.html">
<div>
- controller: {{name}}<br />
- Book Id: {{params.bookId}}<br />
+ controller: {{book.name}}<br />
+ Book Id: {{book.params.bookId}}<br />
</div>
</file>
<file name="chapter.html">
<div>
- controller: {{name}}<br />
- Book Id: {{params.bookId}}<br />
- Chapter Id: {{params.chapterId}}
+ controller: {{chapter.name}}<br />
+ Book Id: {{chapter.params.bookId}}<br />
+ Chapter Id: {{chapter.params.chapterId}}
</div>
</file>
@@ -104,31 +104,33 @@
angular.module('ngView', [], function($routeProvider, $locationProvider) {
$routeProvider.when('/Book/:bookId', {
templateUrl: 'book.html',
- controller: BookCntl
+ controller: BookCntl,
+ controllerAlias: 'book'
});
$routeProvider.when('/Book/:bookId/ch/:chapterId', {
templateUrl: 'chapter.html',
- controller: ChapterCntl
+ controller: ChapterCntl,
+ controllerAlias: 'chapter'
});
// configure html5 to get links working on jsfiddle
$locationProvider.html5Mode(true);
});
- function MainCntl($scope, $route, $routeParams, $location) {
- $scope.$route = $route;
- $scope.$location = $location;
- $scope.$routeParams = $routeParams;
+ function MainCntl($route, $routeParams, $location) {
+ this.$route = $route;
+ this.$location = $location;
+ this.$routeParams = $routeParams;
}
- function BookCntl($scope, $routeParams) {
- $scope.name = "BookCntl";
- $scope.params = $routeParams;
+ function BookCntl($routeParams) {
+ this.name = "BookCntl";
+ this.params = $routeParams;
}
- function ChapterCntl($scope, $routeParams) {
- $scope.name = "ChapterCntl";
- $scope.params = $routeParams;
+ function ChapterCntl($routeParams) {
+ this.name = "ChapterCntl";
+ this.params = $routeParams;
}
</file>
@@ -202,6 +204,9 @@ var ngViewDirective = ['$http', '$templateCache', '$route', '$anchorScroll', '$c
if (current.controller) {
locals.$scope = lastScope;
controller = $controller(current.controller, locals);
+ if (current.controllerAlias) {
+ lastScope[current.controllerAlias] = controller;
+ }
element.children().data('$ngControllerController', controller);
}