|
23 | 23 | * @example
|
24 | 24 | <example module="ngView" animations="true">
|
25 | 25 | <file name="index.html">
|
26 |
| - <div ng-controller="MainCntl"> |
| 26 | + <div ng-controller="MainCntl as main"> |
27 | 27 | Choose:
|
28 | 28 | <a href="Book/Moby">Moby</a> |
|
29 | 29 | <a href="Book/Moby/ch/1">Moby: Ch1</a> |
|
|
37 | 37 | ng-animate="{enter: 'example-enter', leave: 'example-leave'}"></div>
|
38 | 38 | <hr />
|
39 | 39 |
|
40 |
| - <pre>$location.path() = {{$location.path()}}</pre> |
41 |
| - <pre>$route.current.templateUrl = {{$route.current.templateUrl}}</pre> |
42 |
| - <pre>$route.current.params = {{$route.current.params}}</pre> |
43 |
| - <pre>$route.current.scope.name = {{$route.current.scope.name}}</pre> |
44 |
| - <pre>$routeParams = {{$routeParams}}</pre> |
| 40 | + <pre>$location.path() = {{main.$location.path()}}</pre> |
| 41 | + <pre>$route.current.templateUrl = {{main.$route.current.templateUrl}}</pre> |
| 42 | + <pre>$route.current.params = {{main.$route.current.params}}</pre> |
| 43 | + <pre>$route.current.scope.name = {{main.$route.current.scope.name}}</pre> |
| 44 | + <pre>$routeParams = {{main.$routeParams}}</pre> |
45 | 45 | </div>
|
46 | 46 | </file>
|
47 | 47 |
|
48 | 48 | <file name="book.html">
|
49 | 49 | <div>
|
50 |
| - controller: {{name}}<br /> |
51 |
| - Book Id: {{params.bookId}}<br /> |
| 50 | + controller: {{book.name}}<br /> |
| 51 | + Book Id: {{book.params.bookId}}<br /> |
52 | 52 | </div>
|
53 | 53 | </file>
|
54 | 54 |
|
55 | 55 | <file name="chapter.html">
|
56 | 56 | <div>
|
57 |
| - controller: {{name}}<br /> |
58 |
| - Book Id: {{params.bookId}}<br /> |
59 |
| - Chapter Id: {{params.chapterId}} |
| 57 | + controller: {{chapter.name}}<br /> |
| 58 | + Book Id: {{chapter.params.bookId}}<br /> |
| 59 | + Chapter Id: {{chapter.params.chapterId}} |
60 | 60 | </div>
|
61 | 61 | </file>
|
62 | 62 |
|
|
104 | 104 | angular.module('ngView', [], function($routeProvider, $locationProvider) {
|
105 | 105 | $routeProvider.when('/Book/:bookId', {
|
106 | 106 | templateUrl: 'book.html',
|
107 |
| - controller: BookCntl |
| 107 | + controller: BookCntl, |
| 108 | + controllerAlias: 'book' |
108 | 109 | });
|
109 | 110 | $routeProvider.when('/Book/:bookId/ch/:chapterId', {
|
110 | 111 | templateUrl: 'chapter.html',
|
111 |
| - controller: ChapterCntl |
| 112 | + controller: ChapterCntl, |
| 113 | + controllerAlias: 'chapter' |
112 | 114 | });
|
113 | 115 |
|
114 | 116 | // configure html5 to get links working on jsfiddle
|
115 | 117 | $locationProvider.html5Mode(true);
|
116 | 118 | });
|
117 | 119 |
|
118 |
| - function MainCntl($scope, $route, $routeParams, $location) { |
119 |
| - $scope.$route = $route; |
120 |
| - $scope.$location = $location; |
121 |
| - $scope.$routeParams = $routeParams; |
| 120 | + function MainCntl($route, $routeParams, $location) { |
| 121 | + this.$route = $route; |
| 122 | + this.$location = $location; |
| 123 | + this.$routeParams = $routeParams; |
122 | 124 | }
|
123 | 125 |
|
124 |
| - function BookCntl($scope, $routeParams) { |
125 |
| - $scope.name = "BookCntl"; |
126 |
| - $scope.params = $routeParams; |
| 126 | + function BookCntl($routeParams) { |
| 127 | + this.name = "BookCntl"; |
| 128 | + this.params = $routeParams; |
127 | 129 | }
|
128 | 130 |
|
129 |
| - function ChapterCntl($scope, $routeParams) { |
130 |
| - $scope.name = "ChapterCntl"; |
131 |
| - $scope.params = $routeParams; |
| 131 | + function ChapterCntl($routeParams) { |
| 132 | + this.name = "ChapterCntl"; |
| 133 | + this.params = $routeParams; |
132 | 134 | }
|
133 | 135 | </file>
|
134 | 136 |
|
@@ -202,6 +204,9 @@ var ngViewDirective = ['$http', '$templateCache', '$route', '$anchorScroll', '$c
|
202 | 204 | if (current.controller) {
|
203 | 205 | locals.$scope = lastScope;
|
204 | 206 | controller = $controller(current.controller, locals);
|
| 207 | + if (current.controllerAlias) { |
| 208 | + lastScope[current.controllerAlias] = controller; |
| 209 | + } |
205 | 210 | element.children().data('$ngControllerController', controller);
|
206 | 211 | }
|
207 | 212 |
|
|
0 commit comments