diff options
| author | Misko Hevery | 2012-06-06 15:54:40 -0700 |
|---|---|---|
| committer | Misko Hevery | 2013-04-22 23:28:41 -0700 |
| commit | cd38cbf975b501d846e6149d1d993972a1af0053 (patch) | |
| tree | 3857a2879f782c6b7f1350fcd5c8d52e082fd160 /src/ng/directive/ngView.js | |
| parent | 021bdf3922b6525bd117e59fb4945b30a5a55341 (diff) | |
| download | angular.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.js | 51 |
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); } |
