From cd38cbf975b501d846e6149d1d993972a1af0053 Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Wed, 6 Jun 2012 15:54:40 -0700 Subject: 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: '...'}); }); ```` --- src/ng/directive/ngView.js | 51 +++++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 23 deletions(-) (limited to 'src/ng/directive/ngView.js') 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 -
+
Choose: Moby | Moby: Ch1 | @@ -37,26 +37,26 @@ ng-animate="{enter: 'example-enter', leave: 'example-leave'}">

-
$location.path() = {{$location.path()}}
-
$route.current.templateUrl = {{$route.current.templateUrl}}
-
$route.current.params = {{$route.current.params}}
-
$route.current.scope.name = {{$route.current.scope.name}}
-
$routeParams = {{$routeParams}}
+
$location.path() = {{main.$location.path()}}
+
$route.current.templateUrl = {{main.$route.current.templateUrl}}
+
$route.current.params = {{main.$route.current.params}}
+
$route.current.scope.name = {{main.$route.current.scope.name}}
+
$routeParams = {{main.$routeParams}}
- controller: {{name}}
- Book Id: {{params.bookId}}
+ controller: {{book.name}}
+ Book Id: {{book.params.bookId}}
- controller: {{name}}
- Book Id: {{params.bookId}}
- Chapter Id: {{params.chapterId}} + controller: {{chapter.name}}
+ Book Id: {{chapter.params.bookId}}
+ Chapter Id: {{chapter.params.chapterId}}
@@ -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; } @@ -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); } -- cgit v1.2.3