2 * @license AngularJS v1.2.26
3 * (c) 2010-2014 Google, Inc. http://angularjs.org
6 (function(window, angular, undefined) {'use strict';
15 * The `ngRoute` module provides routing and deeplinking services and directives for angular apps.
18 * See {@link ngRoute.$route#example $route} for an example of configuring and using `ngRoute`.
21 * <div doc-module-components="ngRoute"></div>
23 /* global -ngRouteModule */
24 var ngRouteModule = angular.module('ngRoute', ['ng']).
25 provider('$route', $RouteProvider);
29 * @name $routeProvider
34 * Used for configuring routes.
37 * See {@link ngRoute.$route#example $route} for an example of configuring and using `ngRoute`.
40 * Requires the {@link ngRoute `ngRoute`} module to be installed.
42 function $RouteProvider(){
43 function inherit(parent, extra) {
44 return angular.extend(new (angular.extend(function() {}, {prototype:parent}))(), extra);
51 * @name $routeProvider#when
53 * @param {string} path Route path (matched against `$location.path`). If `$location.path`
54 * contains redundant trailing slash or is missing one, the route will still match and the
55 * `$location.path` will be updated to add or drop the trailing slash to exactly match the
58 * * `path` can contain named groups starting with a colon: e.g. `:name`. All characters up
59 * to the next slash are matched and stored in `$routeParams` under the given `name`
60 * when the route matches.
61 * * `path` can contain named groups starting with a colon and ending with a star:
62 * e.g.`:name*`. All characters are eagerly stored in `$routeParams` under the given `name`
63 * when the route matches.
64 * * `path` can contain optional named groups with a question mark: e.g.`:name?`.
66 * For example, routes like `/color/:color/largecode/:largecode*\/edit` will match
67 * `/color/brown/largecode/code/with/slashes/edit` and extract:
70 * * `largecode: code/with/slashes`.
73 * @param {Object} route Mapping information to be assigned to `$route.current` on route
78 * - `controller` – `{(string|function()=}` – Controller fn that should be associated with
79 * newly created scope or the name of a {@link angular.Module#controller registered
80 * controller} if passed as a string.
81 * - `controllerAs` – `{string=}` – A controller alias name. If present the controller will be
82 * published to scope under the `controllerAs` name.
83 * - `template` – `{string=|function()=}` – html template as a string or a function that
84 * returns an html template as a string which should be used by {@link
85 * ngRoute.directive:ngView ngView} or {@link ng.directive:ngInclude ngInclude} directives.
86 * This property takes precedence over `templateUrl`.
88 * If `template` is a function, it will be called with the following parameters:
90 * - `{Array.<Object>}` - route parameters extracted from the current
91 * `$location.path()` by applying the current route
93 * - `templateUrl` – `{string=|function()=}` – path or function that returns a path to an html
94 * template that should be used by {@link ngRoute.directive:ngView ngView}.
96 * If `templateUrl` is a function, it will be called with the following parameters:
98 * - `{Array.<Object>}` - route parameters extracted from the current
99 * `$location.path()` by applying the current route
101 * - `resolve` - `{Object.<string, function>=}` - An optional map of dependencies which should
102 * be injected into the controller. If any of these dependencies are promises, the router
103 * will wait for them all to be resolved or one to be rejected before the controller is
105 * If all the promises are resolved successfully, the values of the resolved promises are
106 * injected and {@link ngRoute.$route#$routeChangeSuccess $routeChangeSuccess} event is
107 * fired. If any of the promises are rejected the
108 * {@link ngRoute.$route#$routeChangeError $routeChangeError} event is fired. The map object
111 * - `key` – `{string}`: a name of a dependency to be injected into the controller.
112 * - `factory` - `{string|function}`: If `string` then it is an alias for a service.
113 * Otherwise if function, then it is {@link auto.$injector#invoke injected}
114 * and the return value is treated as the dependency. If the result is a promise, it is
115 * resolved before its value is injected into the controller. Be aware that
116 * `ngRoute.$routeParams` will still refer to the previous route within these resolve
117 * functions. Use `$route.current.params` to access the new route parameters, instead.
119 * - `redirectTo` – {(string|function())=} – value to update
120 * {@link ng.$location $location} path with and trigger route redirection.
122 * If `redirectTo` is a function, it will be called with the following parameters:
124 * - `{Object.<string>}` - route parameters extracted from the current
125 * `$location.path()` by applying the current route templateUrl.
126 * - `{string}` - current `$location.path()`
127 * - `{Object}` - current `$location.search()`
129 * The custom `redirectTo` function is expected to return a string which will be used
130 * to update `$location.path()` and `$location.search()`.
132 * - `[reloadOnSearch=true]` - {boolean=} - reload route when only `$location.search()`
133 * or `$location.hash()` changes.
135 * If the option is set to `false` and url in the browser changes, then
136 * `$routeUpdate` event is broadcasted on the root scope.
138 * - `[caseInsensitiveMatch=false]` - {boolean=} - match routes without being case sensitive
140 * If the option is set to `true`, then the particular route can be matched without being
143 * @returns {Object} self
146 * Adds a new route definition to the `$route` service.
148 this.when = function(path, route) {
149 routes[path] = angular.extend(
150 {reloadOnSearch: true},
152 path && pathRegExp(path, route)
155 // create redirection for trailing slashes
157 var redirectPath = (path[path.length-1] == '/')
158 ? path.substr(0, path.length-1)
161 routes[redirectPath] = angular.extend(
163 pathRegExp(redirectPath, route)
171 * @param path {string} path
172 * @param opts {Object} options
176 * Normalizes the given path, returning a regular expression
177 * and the original path.
179 * Inspired by pathRexp in visionmedia/express/lib/utils.js.
181 function pathRegExp(path, opts) {
182 var insensitive = opts.caseInsensitiveMatch,
187 keys = ret.keys = [];
190 .replace(/([().])/g, '\\$1')
191 .replace(/(\/)?:(\w+)([\?\*])?/g, function(_, slash, key, option){
192 var optional = option === '?' ? option : null;
193 var star = option === '*' ? option : null;
194 keys.push({ name: key, optional: !!optional });
197 + (optional ? '' : slash)
199 + (optional ? slash : '')
200 + (star && '(.+?)' || '([^/]+)')
205 .replace(/([\/$\*])/g, '\\$1');
207 ret.regexp = new RegExp('^' + path + '$', insensitive ? 'i' : '');
213 * @name $routeProvider#otherwise
216 * Sets route definition that will be used on route change when no other route definition
219 * @param {Object} params Mapping information to be assigned to `$route.current`.
220 * @returns {Object} self
222 this.otherwise = function(params) {
223 this.when(null, params);
228 this.$get = ['$rootScope',
236 function($rootScope, $location, $routeParams, $q, $injector, $http, $templateCache, $sce) {
241 * @requires $location
242 * @requires $routeParams
244 * @property {Object} current Reference to the current route definition.
245 * The route definition contains:
247 * - `controller`: The controller constructor as define in route definition.
248 * - `locals`: A map of locals which is used by {@link ng.$controller $controller} service for
249 * controller instantiation. The `locals` contain
250 * the resolved values of the `resolve` map. Additionally the `locals` also contain:
252 * - `$scope` - The current route scope.
253 * - `$template` - The current route template HTML.
255 * @property {Object} routes Object with all route configuration Objects as its properties.
258 * `$route` is used for deep-linking URLs to controllers and views (HTML partials).
259 * It watches `$location.url()` and tries to map the path to an existing route definition.
261 * Requires the {@link ngRoute `ngRoute`} module to be installed.
263 * You can define routes through {@link ngRoute.$routeProvider $routeProvider}'s API.
265 * The `$route` service is typically used in conjunction with the
266 * {@link ngRoute.directive:ngView `ngView`} directive and the
267 * {@link ngRoute.$routeParams `$routeParams`} service.
270 * This example shows how changing the URL hash causes the `$route` to match a route against the
271 * URL, and the `ngView` pulls in the partial.
273 * <example name="$route-service" module="ngRouteExample"
274 * deps="angular-route.js" fixBase="true">
275 * <file name="index.html">
276 * <div ng-controller="MainController">
278 * <a href="Book/Moby">Moby</a> |
279 * <a href="Book/Moby/ch/1">Moby: Ch1</a> |
280 * <a href="Book/Gatsby">Gatsby</a> |
281 * <a href="Book/Gatsby/ch/4?key=value">Gatsby: Ch4</a> |
282 * <a href="Book/Scarlet">Scarlet Letter</a><br/>
284 * <div ng-view></div>
288 * <pre>$location.path() = {{$location.path()}}</pre>
289 * <pre>$route.current.templateUrl = {{$route.current.templateUrl}}</pre>
290 * <pre>$route.current.params = {{$route.current.params}}</pre>
291 * <pre>$route.current.scope.name = {{$route.current.scope.name}}</pre>
292 * <pre>$routeParams = {{$routeParams}}</pre>
296 * <file name="book.html">
297 * controller: {{name}}<br />
298 * Book Id: {{params.bookId}}<br />
301 * <file name="chapter.html">
302 * controller: {{name}}<br />
303 * Book Id: {{params.bookId}}<br />
304 * Chapter Id: {{params.chapterId}}
307 * <file name="script.js">
308 * angular.module('ngRouteExample', ['ngRoute'])
310 * .controller('MainController', function($scope, $route, $routeParams, $location) {
311 * $scope.$route = $route;
312 * $scope.$location = $location;
313 * $scope.$routeParams = $routeParams;
316 * .controller('BookController', function($scope, $routeParams) {
317 * $scope.name = "BookController";
318 * $scope.params = $routeParams;
321 * .controller('ChapterController', function($scope, $routeParams) {
322 * $scope.name = "ChapterController";
323 * $scope.params = $routeParams;
326 * .config(function($routeProvider, $locationProvider) {
328 * .when('/Book/:bookId', {
329 * templateUrl: 'book.html',
330 * controller: 'BookController',
332 * // I will cause a 1 second delay
333 * delay: function($q, $timeout) {
334 * var delay = $q.defer();
335 * $timeout(delay.resolve, 1000);
336 * return delay.promise;
340 * .when('/Book/:bookId/ch/:chapterId', {
341 * templateUrl: 'chapter.html',
342 * controller: 'ChapterController'
345 * // configure html5 to get links working on jsfiddle
346 * $locationProvider.html5Mode(true);
351 * <file name="protractor.js" type="protractor">
352 * it('should load and compile correct template', function() {
353 * element(by.linkText('Moby: Ch1')).click();
354 * var content = element(by.css('[ng-view]')).getText();
355 * expect(content).toMatch(/controller\: ChapterController/);
356 * expect(content).toMatch(/Book Id\: Moby/);
357 * expect(content).toMatch(/Chapter Id\: 1/);
359 * element(by.partialLinkText('Scarlet')).click();
361 * content = element(by.css('[ng-view]')).getText();
362 * expect(content).toMatch(/controller\: BookController/);
363 * expect(content).toMatch(/Book Id\: Scarlet/);
371 * @name $route#$routeChangeStart
372 * @eventType broadcast on root scope
374 * Broadcasted before a route change. At this point the route services starts
375 * resolving all of the dependencies needed for the route change to occur.
376 * Typically this involves fetching the view template as well as any dependencies
377 * defined in `resolve` route property. Once all of the dependencies are resolved
378 * `$routeChangeSuccess` is fired.
380 * @param {Object} angularEvent Synthetic event object.
381 * @param {Route} next Future route information.
382 * @param {Route} current Current route information.
387 * @name $route#$routeChangeSuccess
388 * @eventType broadcast on root scope
390 * Broadcasted after a route dependencies are resolved.
391 * {@link ngRoute.directive:ngView ngView} listens for the directive
392 * to instantiate the controller and render the view.
394 * @param {Object} angularEvent Synthetic event object.
395 * @param {Route} current Current route information.
396 * @param {Route|Undefined} previous Previous route information, or undefined if current is
397 * first route entered.
402 * @name $route#$routeChangeError
403 * @eventType broadcast on root scope
405 * Broadcasted if any of the resolve promises are rejected.
407 * @param {Object} angularEvent Synthetic event object
408 * @param {Route} current Current route information.
409 * @param {Route} previous Previous route information.
410 * @param {Route} rejection Rejection of the promise. Usually the error of the failed promise.
415 * @name $route#$routeUpdate
416 * @eventType broadcast on root scope
419 * The `reloadOnSearch` property has been set to false, and we are reusing the same
420 * instance of the Controller.
423 var forceReload = false,
429 * @name $route#reload
432 * Causes `$route` service to reload the current route even if
433 * {@link ng.$location $location} hasn't changed.
435 * As a result of that, {@link ngRoute.directive:ngView ngView}
436 * creates new scope, reinstantiates the controller.
440 $rootScope.$evalAsync(updateRoute);
444 $rootScope.$on('$locationChangeSuccess', updateRoute);
448 /////////////////////////////////////////////////////
451 * @param on {string} current url
452 * @param route {Object} route regexp to match the url against
456 * Check if the route matches the current url.
458 * Inspired by match in
459 * visionmedia/express/lib/router/router.js.
461 function switchRouteMatcher(on, route) {
462 var keys = route.keys,
465 if (!route.regexp) return null;
467 var m = route.regexp.exec(on);
470 for (var i = 1, len = m.length; i < len; ++i) {
471 var key = keys[i - 1];
476 params[key.name] = val;
482 function updateRoute() {
483 var next = parseRoute(),
484 last = $route.current;
486 if (next && last && next.$$route === last.$$route
487 && angular.equals(next.pathParams, last.pathParams)
488 && !next.reloadOnSearch && !forceReload) {
489 last.params = next.params;
490 angular.copy(last.params, $routeParams);
491 $rootScope.$broadcast('$routeUpdate', last);
492 } else if (next || last) {
494 $rootScope.$broadcast('$routeChangeStart', next, last);
495 $route.current = next;
497 if (next.redirectTo) {
498 if (angular.isString(next.redirectTo)) {
499 $location.path(interpolate(next.redirectTo, next.params)).search(next.params)
502 $location.url(next.redirectTo(next.pathParams, $location.path(), $location.search()))
511 var locals = angular.extend({}, next.resolve),
512 template, templateUrl;
514 angular.forEach(locals, function(value, key) {
515 locals[key] = angular.isString(value) ?
516 $injector.get(value) : $injector.invoke(value);
519 if (angular.isDefined(template = next.template)) {
520 if (angular.isFunction(template)) {
521 template = template(next.params);
523 } else if (angular.isDefined(templateUrl = next.templateUrl)) {
524 if (angular.isFunction(templateUrl)) {
525 templateUrl = templateUrl(next.params);
527 templateUrl = $sce.getTrustedResourceUrl(templateUrl);
528 if (angular.isDefined(templateUrl)) {
529 next.loadedTemplateUrl = templateUrl;
530 template = $http.get(templateUrl, {cache: $templateCache}).
531 then(function(response) { return response.data; });
534 if (angular.isDefined(template)) {
535 locals['$template'] = template;
537 return $q.all(locals);
540 // after route change
541 then(function(locals) {
542 if (next == $route.current) {
544 next.locals = locals;
545 angular.copy(next.params, $routeParams);
547 $rootScope.$broadcast('$routeChangeSuccess', next, last);
550 if (next == $route.current) {
551 $rootScope.$broadcast('$routeChangeError', next, last, error);
559 * @returns {Object} the current active route, by matching it against the URL
561 function parseRoute() {
564 angular.forEach(routes, function(route, path) {
565 if (!match && (params = switchRouteMatcher($location.path(), route))) {
566 match = inherit(route, {
567 params: angular.extend({}, $location.search(), params),
568 pathParams: params});
569 match.$$route = route;
572 // No route matched; fallback to "otherwise" route
573 return match || routes[null] && inherit(routes[null], {params: {}, pathParams:{}});
577 * @returns {string} interpolation of the redirect path with the parameters
579 function interpolate(string, params) {
581 angular.forEach((string||'').split(':'), function(segment, i) {
583 result.push(segment);
585 var segmentMatch = segment.match(/(\w+)(.*)/);
586 var key = segmentMatch[1];
587 result.push(params[key]);
588 result.push(segmentMatch[2] || '');
592 return result.join('');
597 ngRouteModule.provider('$routeParams', $RouteParamsProvider);
606 * The `$routeParams` service allows you to retrieve the current set of route parameters.
608 * Requires the {@link ngRoute `ngRoute`} module to be installed.
610 * The route parameters are a combination of {@link ng.$location `$location`}'s
611 * {@link ng.$location#search `search()`} and {@link ng.$location#path `path()`}.
612 * The `path` parameters are extracted when the {@link ngRoute.$route `$route`} path is matched.
614 * In case of parameter name collision, `path` params take precedence over `search` params.
616 * The service guarantees that the identity of the `$routeParams` object will remain unchanged
617 * (but its properties will likely change) even when a route change occurs.
619 * Note that the `$routeParams` are only updated *after* a route change completes successfully.
620 * This means that you cannot rely on `$routeParams` being correct in route resolve functions.
621 * Instead you can use `$route.current.params` to access the new route's parameters.
626 * // URL: http://server.com/index.html#/Chapter/1/Section/2?search=moby
627 * // Route: /Chapter/:chapterId/Section/:sectionId
630 * $routeParams ==> {chapterId:'1', sectionId:'2', search:'moby'}
633 function $RouteParamsProvider() {
634 this.$get = function() { return {}; };
637 ngRouteModule.directive('ngView', ngViewFactory);
638 ngRouteModule.directive('ngView', ngViewFillContentFactory);
648 * `ngView` is a directive that complements the {@link ngRoute.$route $route} service by
649 * including the rendered template of the current route into the main layout (`index.html`) file.
650 * Every time the current route changes, the included view changes with it according to the
651 * configuration of the `$route` service.
653 * Requires the {@link ngRoute `ngRoute`} module to be installed.
656 * enter - animation is used to bring new content into the browser.
657 * leave - animation is used to animate existing content away.
659 * The enter and leave animation occur concurrently.
663 * @param {string=} onload Expression to evaluate whenever the view updates.
665 * @param {string=} autoscroll Whether `ngView` should call {@link ng.$anchorScroll
666 * $anchorScroll} to scroll the viewport after the view is updated.
668 * - If the attribute is not set, disable scrolling.
669 * - If the attribute is set without value, enable scrolling.
670 * - Otherwise enable scrolling only if the `autoscroll` attribute value evaluated
671 * as an expression yields a truthy value.
673 <example name="ngView-directive" module="ngViewExample"
674 deps="angular-route.js;angular-animate.js"
675 animations="true" fixBase="true">
676 <file name="index.html">
677 <div ng-controller="MainCtrl as main">
679 <a href="Book/Moby">Moby</a> |
680 <a href="Book/Moby/ch/1">Moby: Ch1</a> |
681 <a href="Book/Gatsby">Gatsby</a> |
682 <a href="Book/Gatsby/ch/4?key=value">Gatsby: Ch4</a> |
683 <a href="Book/Scarlet">Scarlet Letter</a><br/>
685 <div class="view-animate-container">
686 <div ng-view class="view-animate"></div>
690 <pre>$location.path() = {{main.$location.path()}}</pre>
691 <pre>$route.current.templateUrl = {{main.$route.current.templateUrl}}</pre>
692 <pre>$route.current.params = {{main.$route.current.params}}</pre>
693 <pre>$route.current.scope.name = {{main.$route.current.scope.name}}</pre>
694 <pre>$routeParams = {{main.$routeParams}}</pre>
698 <file name="book.html">
700 controller: {{book.name}}<br />
701 Book Id: {{book.params.bookId}}<br />
705 <file name="chapter.html">
707 controller: {{chapter.name}}<br />
708 Book Id: {{chapter.params.bookId}}<br />
709 Chapter Id: {{chapter.params.chapterId}}
713 <file name="animations.css">
714 .view-animate-container {
716 height:100px!important;
719 border:1px solid black;
728 .view-animate.ng-enter, .view-animate.ng-leave {
729 -webkit-transition:all cubic-bezier(0.250, 0.460, 0.450, 0.940) 1.5s;
730 transition:all cubic-bezier(0.250, 0.460, 0.450, 0.940) 1.5s;
734 border-left:1px solid black;
744 .view-animate.ng-enter {
747 .view-animate.ng-enter.ng-enter-active {
750 .view-animate.ng-leave.ng-leave-active {
755 <file name="script.js">
756 angular.module('ngViewExample', ['ngRoute', 'ngAnimate'])
757 .config(['$routeProvider', '$locationProvider',
758 function($routeProvider, $locationProvider) {
760 .when('/Book/:bookId', {
761 templateUrl: 'book.html',
762 controller: 'BookCtrl',
765 .when('/Book/:bookId/ch/:chapterId', {
766 templateUrl: 'chapter.html',
767 controller: 'ChapterCtrl',
768 controllerAs: 'chapter'
771 $locationProvider.html5Mode(true);
773 .controller('MainCtrl', ['$route', '$routeParams', '$location',
774 function($route, $routeParams, $location) {
775 this.$route = $route;
776 this.$location = $location;
777 this.$routeParams = $routeParams;
779 .controller('BookCtrl', ['$routeParams', function($routeParams) {
780 this.name = "BookCtrl";
781 this.params = $routeParams;
783 .controller('ChapterCtrl', ['$routeParams', function($routeParams) {
784 this.name = "ChapterCtrl";
785 this.params = $routeParams;
790 <file name="protractor.js" type="protractor">
791 it('should load and compile correct template', function() {
792 element(by.linkText('Moby: Ch1')).click();
793 var content = element(by.css('[ng-view]')).getText();
794 expect(content).toMatch(/controller\: ChapterCtrl/);
795 expect(content).toMatch(/Book Id\: Moby/);
796 expect(content).toMatch(/Chapter Id\: 1/);
798 element(by.partialLinkText('Scarlet')).click();
800 content = element(by.css('[ng-view]')).getText();
801 expect(content).toMatch(/controller\: BookCtrl/);
802 expect(content).toMatch(/Book Id\: Scarlet/);
811 * @name ngView#$viewContentLoaded
812 * @eventType emit on the current ngView scope
814 * Emitted every time the ngView content is reloaded.
816 ngViewFactory.$inject = ['$route', '$anchorScroll', '$animate'];
817 function ngViewFactory( $route, $anchorScroll, $animate) {
822 transclude: 'element',
823 link: function(scope, $element, attr, ctrl, $transclude) {
827 autoScrollExp = attr.autoscroll,
828 onloadExp = attr.onload || '';
830 scope.$on('$routeChangeSuccess', update);
833 function cleanupLastView() {
834 if(previousElement) {
835 previousElement.remove();
836 previousElement = null;
839 currentScope.$destroy();
843 $animate.leave(currentElement, function() {
844 previousElement = null;
846 previousElement = currentElement;
847 currentElement = null;
852 var locals = $route.current && $route.current.locals,
853 template = locals && locals.$template;
855 if (angular.isDefined(template)) {
856 var newScope = scope.$new();
857 var current = $route.current;
859 // Note: This will also link all children of ng-view that were contained in the original
860 // html. If that content contains controllers, ... they could pollute/change the scope.
861 // However, using ng-view on an element with additional content does not make sense...
862 // Note: We can't remove them in the cloneAttchFn of $transclude as that
863 // function is called before linking the content, which would apply child
864 // directives to non existing elements.
865 var clone = $transclude(newScope, function(clone) {
866 $animate.enter(clone, null, currentElement || $element, function onNgViewEnter () {
867 if (angular.isDefined(autoScrollExp)
868 && (!autoScrollExp || scope.$eval(autoScrollExp))) {
875 currentElement = clone;
876 currentScope = current.scope = newScope;
877 currentScope.$emit('$viewContentLoaded');
878 currentScope.$eval(onloadExp);
887 // This directive is called during the $transclude call of the first `ngView` directive.
888 // It will replace and compile the content of the element with the loaded template.
889 // We need this directive so that the element content is already filled when
890 // the link function of another directive on the same element as ngView
892 ngViewFillContentFactory.$inject = ['$compile', '$controller', '$route'];
893 function ngViewFillContentFactory($compile, $controller, $route) {
897 link: function(scope, $element) {
898 var current = $route.current,
899 locals = current.locals;
901 $element.html(locals.$template);
903 var link = $compile($element.contents());
905 if (current.controller) {
906 locals.$scope = scope;
907 var controller = $controller(current.controller, locals);
908 if (current.controllerAs) {
909 scope[current.controllerAs] = controller;
911 $element.data('$ngControllerController', controller);
912 $element.children().data('$ngControllerController', controller);
921 })(window, window.angular);