1/** 2 * @license AngularJS v1.5.7 3 * (c) 2010-2016 Google, Inc. http://angularjs.org 4 * License: MIT 5 */ 6(function(window, angular) {'use strict'; 7 8/* global shallowCopy: true */ 9 10/** 11 * Creates a shallow copy of an object, an array or a primitive. 12 * 13 * Assumes that there are no proto properties for objects. 14 */ 15function shallowCopy(src, dst) { 16 if (isArray(src)) { 17 dst = dst || []; 18 19 for (var i = 0, ii = src.length; i < ii; i++) { 20 dst[i] = src[i]; 21 } 22 } else if (isObject(src)) { 23 dst = dst || {}; 24 25 for (var key in src) { 26 if (!(key.charAt(0) === '$' && key.charAt(1) === '$')) { 27 dst[key] = src[key]; 28 } 29 } 30 } 31 32 return dst || src; 33} 34 35/* global shallowCopy: false */ 36 37// There are necessary for `shallowCopy()` (included via `src/shallowCopy.js`) 38var isArray = angular.isArray; 39var isObject = angular.isObject; 40 41/** 42 * @ngdoc module 43 * @name ngRoute 44 * @description 45 * 46 * # ngRoute 47 * 48 * The `ngRoute` module provides routing and deeplinking services and directives for angular apps. 49 * 50 * ## Example 51 * See {@link ngRoute.$route#example $route} for an example of configuring and using `ngRoute`. 52 * 53 * 54 * <div doc-module-components="ngRoute"></div> 55 */ 56 /* global -ngRouteModule */ 57var ngRouteModule = angular.module('ngRoute', ['ng']). 58 provider('$route', $RouteProvider), 59 $routeMinErr = angular.$$minErr('ngRoute'); 60 61/** 62 * @ngdoc provider 63 * @name $routeProvider 64 * 65 * @description 66 *
67 * Used for configuring routes. 68 * 69 * ## Example 70 * See {@link ngRoute.$route#example $route} for an example of configuring and using `ngRoute`. 71 * 72 * ## Dependencies 73 * Requires the {@link ngRoute `ngRoute`} module to be installed. 74 */ 75function $RouteProvider() { 76 function inherit(parent, extra) { 77 return angular.extend(Object.create(parent), extra); 78 } 79 80 var routes = {}; 81 82 /** 83 * @ngdoc method 84 * @name $routeProvider#when 85 * 86 * @param {string} path Route path (matched against `$location.path`). If `$location.path` 87 * contains redundant trailing slash or is missing one, the route will still match and the 88 * `$location.path` will be updated to add or drop the trailing slash to exactly match the 89 * route definition. 90 * 91 * * `path` can contain named groups starting with a colon: e.g. `:name`. All characters up 92 * to the next slash are matched and stored in `$routeParams` under the given `name` 93 * when the route matches. 94 * * `path` can contain named groups starting with a colon and ending with a star: 95 * e.g.`:name*`. All characters are eagerly stored in `$routeParams` under the given `name` 96 * when the route matches. 97 * * `path` can contain optional named groups with a question mark: e.g.`:name?`. 98 * 99 * For example, routes like `/color/:color/largecode/:largecode*\/edit` will match 100 * `/color/brown/largecode/code/with/slashes/edit` and extract: 101 * 102 * * `color: brown` 103 * * `largecode: code/with/slashes`. 104 * 105 * 106 * @param {Object} route Mapping information to be assigned to `$route.current` on route 107 * match. 108 * 109 * Object properties: 110 * 111 * - `controller` â `{(string|function()=}` â Controller fn that should be associated with 112 * newly created scope or the name of a {@link angular.Module#controller registered 113 * controller} if passed as a string. 114 * - `controllerAs` â `{string=}` â An identifier name for a reference to the controller. 115 * If present, the controller will be published to scope under the `controllerAs` name. 116 * - `template` â `{string=|function()=}` â html template as a string or a function that 117 * returns an html template as a string which should be used by {@link 118 * ngRoute.directive:ngView ngView} or {@link ng.directive:ngInclude ngInclude} directives. 119 * This property takes precedence over `templateUrl`. 120 * 121 * If `template` is a function, it will be called with the following parameters: 122 * 123 * - `{Array.<Object>}` - route parameters extracted from the current 124 * `$location.path()` by applying the current route 125 * 126 * - `templateUrl` â `{string=|function()=}` â path or function that returns a path to an html 127 * template that should be used by {@link ngRoute.directive:ngView ngView}. 128 * 129 * If `templateUrl` is a function, it will be called with the following parameters: 130 * 131 * - `{Array.<Object>}` - route parameters extracted from the current 132 * `$location.path()` by applying the current route 133 * 134 * - `resolve` - `{Object.<string, function>=}` - An optional map of dependencies which should 135 * be injected into the controller. If any of these dependencies are promises, the router 136 * will wait for them all to be resolved or one to be rejected before the controller is 137 * instantiated. 138 * If all the promises are resolved successfully, the values of the resolved promises are 139 * injected and {@link ngRoute.$route#$routeChangeSuccess $routeChangeSuccess} event is 140 * fired. If any of the promises are rejected the 141 * {@link ngRoute.$route#$routeChangeError $routeChangeError} event is fired. 142 * For easier access to the resolved dependencies from the template, the `resolve` map will 143 * be available on the scope of the route, under `$resolve` (by default) or a custom name 144 * specified by the `resolveAs` property (see below). This can be particularly useful, when 145 * working with {@link angular.Module#component components} as route templates.<br /> 146 * <div class="alert alert-warning"> 147 * **Note:** If your scope already contains a property with this name, it will be hidden 148 * or overwritten. Make sure, you specify an appropriate name for this property, that 149 * does not collide with other properties on the scope. 150 * </div> 151 * The map object is: 152 * 153 * - `key` â `{string}`: a name of a dependency to be injected into the controller. 154 * - `factory` - `{string|function}`: If `string` then it is an alias for a service. 155 * Otherwise if function, then it is {@link auto.$injector#invoke injected} 156 * and the return value is treated as the dependency. If the result is a promise, it is 157 * resolved before its value is injected into the controller. Be aware that
158 * `ngRoute.$routeParams` will still refer to the previous route within these resolve 159 * functions. Use `$route.current.params` to access the new route parameters, instead. 160 * 161 * - `resolveAs` - `{string=}` - The name under which the `resolve` map will be available on 162 * the scope of the route. If omitted, defaults to `$resolve`. 163 * 164 * - `redirectTo` â `{(string|function())=}` â value to update 165 * {@link ng.$location $location} path with and trigger route redirection. 166 * 167 * If `redirectTo` is a function, it will be called with the following parameters: 168 * 169 * - `{Object.<string>}` - route parameters extracted from the current 170 * `$location.path()` by applying the current route templateUrl. 171 * - `{string}` - current `$location.path()` 172 * - `{Object}` - current `$location.search()` 173 * 174 * The custom `redirectTo` function is expected to return a string which will be used 175 * to update `$location.path()` and `$location.search()`. 176 * 177 * - `[reloadOnSearch=true]` - `{boolean=}` - reload route when only `$location.search()` 178 * or `$location.hash()` changes. 179 * 180 * If the option is set to `false` and url in the browser changes, then 181 * `$routeUpdate` event is broadcasted on the root scope. 182 * 183 * - `[caseInsensitiveMatch=false]` - `{boolean=}` - match routes without being case sensitive 184 * 185 * If the option is set to `true`, then the particular route can be matched without being 186 * case sensitive 187 * 188 * @returns {Object} self 189 * 190 * @description 191 * Adds a new route definition to the `$route` service. 192 */ 193 this.when = function(path, route) { 194 //copy original route object to preserve params inherited from proto chain 195 var routeCopy = shallowCopy(route); 196 if (angular.isUndefined(routeCopy.reloadOnSearch)) { 197 routeCopy.reloadOnSearch = true; 198 } 199 if (angular.isUndefined(routeCopy.caseInsensitiveMatch)) { 200 routeCopy.caseInsensitiveMatch = this.caseInsensitiveMatch; 201 } 202 routes[path] = angular.extend( 203 routeCopy, 204 path && pathRegExp(path, routeCopy) 205 ); 206 207 // create redirection for trailing slashes 208 if (path) { 209 var redirectPath = (path[path.length - 1] == '/') 210 ? path.substr(0, path.length - 1) 211 : path + '/'; 212 213 routes[redirectPath] = angular.extend( 214 {redirectTo: path}, 215 pathRegExp(redirectPath, routeCopy) 216 ); 217 } 218 219 return this; 220 }; 221 222 /** 223 * @ngdoc property 224 * @name $routeProvider#caseInsensitiveMatch 225 * @description 226 * 227 * A boolean property indicating if routes defined 228 * using this provider should be matched using a case insensitive 229 * algorithm. Defaults to `false`. 230 */ 231 this.caseInsensitiveMatch = false; 232 233 /** 234 * @param path {string} path 235 * @param opts {Object} options 236 * @return {?Object} 237 * 238 * @description 239 * Normalizes the given path, returning a regular expression 240 * and the original path. 241 * 242 * Inspired by pathRexp in visionmedia/express/lib/utils.js. 243 */ 244 function pathRegExp(path, opts) { 245 var insensitive = opts.caseInsensitiveMatch, 246 ret = { 247 originalPath: path, 248 regexp: path 249 }, 250 keys = ret.keys = []; 251 252 path = path 253 .replace(/([().])/g, '\\$1') 254 .replace(/(\/)?:(\w+)(\*\?|[\?\*])?/g, function(_, slash, key, option) { 255 var optional = (option === '?' || option === '*?') ? '?' : null; 256 var star = (option === '*' || option === '*?') ? '*' : null; 257 keys.push({ name: key, optional: !!optional }); 258 slash = slash || ''; 259 return '' 260 + (optional ? '' : slash) 261 + '(?:' 262 + (optional ? slash : '') 263 + (star && '(.+?)' || '([^/]+)') 264 + (optional || '') 265 + ')' 266 + (optional || ''); 267 }) 268 .replace(/([\/$\*])/g, '\\$1'); 269 270 ret.regexp = new RegExp('^' + path + '$', insensitive ? 'i' : ''); 271 return ret; 272 } 273 274 /** 275 * @ngdoc method 276 * @name $routeProvider#otherwise 277 * 278 * @description 279 * Sets route definition that will be used on route change when no other route definition 280 * is matched. 281 * 282 * @param {Object|string} params Mapping information to be assigned to `$route.current`. 283 * If called with a string, the value maps to `redirectTo`. 284 * @returns {Object} self 285 */ 286 this.otherwise = function(params) { 287 if (typeof params === 'string') { 288 params = {redirectTo: params}; 289 } 290 this.when(null, params); 291 return this; 292 }; 293 294 295 this.$get = ['$rootScope', 296 '$location',
297 '$routeParams', 298 '$q', 299 '$injector', 300 '$templateRequest', 301 '$sce', 302 function($rootScope, $location, $routeParams, $q, $injector, $templateRequest, $sce) { 303 304 /** 305 * @ngdoc service 306 * @name $route 307 * @requires $location 308 * @requires $routeParams 309 * 310 * @property {Object} current Reference to the current route definition. 311 * The route definition contains: 312 * 313 * - `controller`: The controller constructor as defined in the route definition. 314 * - `locals`: A map of locals which is used by {@link ng.$controller $controller} service for 315 * controller instantiation. The `locals` contain 316 * the resolved values of the `resolve` map. Additionally the `locals` also contain: 317 * 318 * - `$scope` - The current route scope. 319 * - `$template` - The current route template HTML. 320 * 321 * The `locals` will be assigned to the route scope's `$resolve` property. You can override 322 * the property name, using `resolveAs` in the route definition. See 323 * {@link ngRoute.$routeProvider $routeProvider} for more info. 324 * 325 * @property {Object} routes Object with all route configuration Objects as its properties. 326 * 327 * @description 328 * `$route` is used for deep-linking URLs to controllers and views (HTML partials). 329 * It watches `$location.url()` and tries to map the path to an existing route definition. 330 * 331 * Requires the {@link ngRoute `ngRoute`} module to be installed. 332 * 333 * You can define routes through {@link ngRoute.$routeProvider $routeProvider}'s API. 334 * 335 * The `$route` service is typically used in conjunction with the 336 * {@link ngRoute.directive:ngView `ngView`} directive and the 337 * {@link ngRoute.$routeParams `$routeParams`} service. 338 * 339 * @example 340 * This example shows how changing the URL hash causes the `$route` to match a route against the 341 * URL, and the `ngView` pulls in the partial. 342 * 343 * <example name="$route-service" module="ngRouteExample" 344 * deps="angular-route.js" fixBase="true"> 345 * <file name="index.html"> 346 * <div ng-controller="MainController"> 347 * Choose: 348 * <a href="Book/Moby">Moby</a> | 349 * <a href="Book/Moby/ch/1">Moby: Ch1</a> | 350 * <a href="Book/Gatsby">Gatsby</a> | 351 * <a href="Book/Gatsby/ch/4?key=value">Gatsby: Ch4</a> | 352 * <a href="Book/Scarlet">Scarlet Letter</a><br/> 353 * 354 * <div ng-view></div> 355 * 356 * <hr /> 357 * 358 * <pre>$location.path() = {{$location.path()}}</pre> 359 * <pre>$route.current.templateUrl = {{$route.current.templateUrl}}</pre> 360 * <pre>$route.current.params = {{$route.current.params}}</pre> 361 * <pre>$route.current.scope.name = {{$route.current.scope.name}}</pre> 362 * <pre>$routeParams = {{$routeParams}}</pre> 363 * </div> 364 * </file> 365 * 366 * <file name="book.html"> 367 * controller: {{name}}<br /> 368 * Book Id: {{params.bookId}}<br /> 369 * </file> 370 * 371 * <file name="chapter.html"> 372 * controller: {{name}}<br /> 373 * Book Id: {{params.bookId}}<br /> 374 * Chapter Id: {{params.chapterId}} 375 * </file> 376 * 377 * <file name="script.js"> 378 * angular.module('ngRouteExample', ['ngRoute']) 379 * 380 * .controller('MainController', function($scope, $route, $routeParams, $location) { 381 * $scope.$route = $route; 382 * $scope.$location = $location; 383 * $scope.$routeParams = $routeParams; 384 * }) 385 * 386 * .controller('BookController', function($scope, $routeParams) { 387 * $scope.name = "BookController"; 388 * $scope.params = $routeParams; 389 * }) 390 * 391 * .controller('ChapterController', function($scope, $routeParams) { 392 * $scope.name = "ChapterController"; 393 * $scope.params = $routeParams; 394 * }) 395 * 396 * .config(function($routeProvider, $locationProvider) { 397 * $routeProvider 398 * .when('/Book/:bookId', { 399 * templateUrl: 'book.html', 400 * controller: 'BookController', 401 * resolve: { 402 * // I will cause a 1 second delay 403 * delay: function($q, $timeout) {
404 * var delay = $q.defer(); 405 * $timeout(delay.resolve, 1000); 406 * return delay.promise; 407 * } 408 * } 409 * }) 410 * .when('/Book/:bookId/ch/:chapterId', { 411 * templateUrl: 'chapter.html', 412 * controller: 'ChapterController' 413 * }); 414 * 415 * // configure html5 to get links working on jsfiddle 416 * $locationProvider.html5Mode(true); 417 * }); 418 * 419 * </file> 420 * 421 * <file name="protractor.js" type="protractor"> 422 * it('should load and compile correct template', function() { 423 * element(by.linkText('Moby: Ch1')).click(); 424 * var content = element(by.css('[ng-view]')).getText(); 425 * expect(content).toMatch(/controller\: ChapterController/); 426 * expect(content).toMatch(/Book Id\: Moby/); 427 * expect(content).toMatch(/Chapter Id\: 1/); 428 * 429 * element(by.partialLinkText('Scarlet')).click(); 430 * 431 * content = element(by.css('[ng-view]')).getText(); 432 * expect(content).toMatch(/controller\: BookController/); 433 * expect(content).toMatch(/Book Id\: Scarlet/); 434 * }); 435 * </file> 436 * </example> 437 */ 438 439 /** 440 * @ngdoc event 441 * @name $route#$routeChangeStart 442 * @eventType broadcast on root scope 443 * @description 444 * Broadcasted before a route change. At this point the route services starts 445 * resolving all of the dependencies needed for the route change to occur. 446 * Typically this involves fetching the view template as well as any dependencies 447 * defined in `resolve` route property. Once all of the dependencies are resolved 448 * `$routeChangeSuccess` is fired. 449 * 450 * The route change (and the `$location` change that triggered it) can be prevented 451 * by calling `preventDefault` method of the event. See {@link ng.$rootScope.Scope#$on} 452 * for more details about event object. 453 * 454 * @param {Object} angularEvent Synthetic event object. 455 * @param {Route} next Future route information. 456 * @param {Route} current Current route information. 457 */ 458 459 /** 460 * @ngdoc event 461 * @name $route#$routeChangeSuccess 462 * @eventType broadcast on root scope 463 * @description 464 * Broadcasted after a route change has happened successfully. 465 * The `resolve` dependencies are now available in the `current.locals` property. 466 * 467 * {@link ngRoute.directive:ngView ngView} listens for the directive 468 * to instantiate the controller and render the view. 469 * 470 * @param {Object} angularEvent Synthetic event object. 471 * @param {Route} current Current route information. 472 * @param {Route|Undefined} previous Previous route information, or undefined if current is 473 * first route entered. 474 */ 475 476 /** 477 * @ngdoc event 478 * @name $route#$routeChangeError 479 * @eventType broadcast on root scope 480 * @description 481 * Broadcasted if any of the resolve promises are rejected. 482 * 483 * @param {Object} angularEvent Synthetic event object 484 * @param {Route} current Current route information. 485 * @param {Route} previous Previous route information. 486 * @param {Route} rejection Rejection of the promise. Usually the error of the failed promise. 487 */ 488 489 /** 490 * @ngdoc event 491 * @name $route#$routeUpdate 492 * @eventType broadcast on root scope 493 * @description 494 * The `reloadOnSearch` property has been set to false, and we are reusing the same 495 * instance of the Controller. 496 * 497 * @param {Object} angularEvent Synthetic event object 498 * @param {Route} current Current/previous route information. 499 */ 500 501 var forceReload = false, 502 preparedRoute, 503 preparedRouteIsUpdateOnly, 504 $route = { 505 routes: routes, 506 507 /** 508 * @ngdoc method 509 * @name $route#reload 510 * 511 * @description 512 * Causes `$route` service to reload the current route even if 513 * {@link ng.$location $location} hasn't changed. 514 * 515 * As a result of that, {@link ngRoute.directive:ngView ngView} 516 * creates new scope and reinstantiates the controller. 517 */ 518 reload: function() { 519 forceReload = true; 520 521 var fakeLocationEvent = { 522 defaultPrevented: false, 523 preventDefault: function fakePreventDefault() { 524 this.defaultPrevented = true; 525 forceReload = false;
526 } 527 }; 528 529 $rootScope.$evalAsync(function() { 530 prepareRoute(fakeLocationEvent); 531 if (!fakeLocationEvent.defaultPrevented) commitRoute(); 532 }); 533 }, 534 535 /** 536 * @ngdoc method 537 * @name $route#updateParams 538 * 539 * @description 540 * Causes `$route` service to update the current URL, replacing 541 * current route parameters with those specified in `newParams`. 542 * Provided property names that match the route's path segment 543 * definitions will be interpolated into the location's path, while 544 * remaining properties will be treated as query params. 545 * 546 * @param {!Object<string, string>} newParams mapping of URL parameter names to values 547 */ 548 updateParams: function(newParams) { 549 if (this.current && this.current.$$route) { 550 newParams = angular.extend({}, this.current.params, newParams); 551 $location.path(interpolate(this.current.$$route.originalPath, newParams)); 552 // interpolate modifies newParams, only query params are left 553 $location.search(newParams); 554 } else { 555 throw $routeMinErr('norout', 'Tried updating route when with no current route'); 556 } 557 } 558 }; 559 560 $rootScope.$on('$locationChangeStart', prepareRoute); 561 $rootScope.$on('$locationChangeSuccess', commitRoute); 562 563 return $route; 564 565 ///////////////////////////////////////////////////// 566 567 /** 568 * @param on {string} current url 569 * @param route {Object} route regexp to match the url against 570 * @return {?Object} 571 * 572 * @description 573 * Check if the route matches the current url. 574 * 575 * Inspired by match in 576 * visionmedia/express/lib/router/router.js. 577 */ 578 function switchRouteMatcher(on, route) { 579 var keys = route.keys, 580 params = {}; 581 582 if (!route.regexp) return null; 583 584 var m = route.regexp.exec(on); 585 if (!m) return null; 586 587 for (var i = 1, len = m.length; i < len; ++i) { 588 var key = keys[i - 1]; 589 590 var val = m[i]; 591 592 if (key && val) { 593 params[key.name] = val; 594 } 595 } 596 return params; 597 } 598 599 function prepareRoute($locationEvent) { 600 var lastRoute = $route.current; 601 602 preparedRoute = parseRoute(); 603 preparedRouteIsUpdateOnly = preparedRoute && lastRoute && preparedRoute.$$route === lastRoute.$$route 604 && angular.equals(preparedRoute.pathParams, lastRoute.pathParams) 605 && !preparedRoute.reloadOnSearch && !forceReload; 606 607 if (!preparedRouteIsUpdateOnly && (lastRoute || preparedRoute)) { 608 if ($rootScope.$broadcast('$routeChangeStart', preparedRoute, lastRoute).defaultPrevented) { 609 if ($locationEvent) { 610 $locationEvent.preventDefault(); 611 } 612 } 613 } 614 } 615 616 function commitRoute() { 617 var lastRoute = $route.current; 618 var nextRoute = preparedRoute; 619 620 if (preparedRouteIsUpdateOnly) { 621 lastRoute.params = nextRoute.params; 622 angular.copy(lastRoute.params, $routeParams); 623 $rootScope.$broadcast('$routeUpdate', lastRoute); 624 } else if (nextRoute || lastRoute) { 625 forceReload = false; 626 $route.current = nextRoute; 627 if (nextRoute) { 628 if (nextRoute.redirectTo) { 629 if (angular.isString(nextRoute.redirectTo)) { 630 $location.path(interpolate(nextRoute.redirectTo, nextRoute.params)).search(nextRoute.params) 631 .replace(); 632 } else { 633 $location.url(nextRoute.redirectTo(nextRoute.pathParams, $location.path(), $location.search())) 634 .replace(); 635 } 636 } 637 } 638 639 $q.when(nextRoute). 640 then(resolveLocals). 641 then(function(locals) { 642 // after route change 643 if (nextRoute == $route.current) { 644 if (nextRoute) { 645 nextRoute.locals = locals;
vendor: 4,123 bytes, lines 646-768
646 angular.copy(nextRoute.params, $routeParams); 647 } 648 $rootScope.$broadcast('$routeChangeSuccess', nextRoute, lastRoute); 649 } 650 }, function(error) { 651 if (nextRoute == $route.current) { 652 $rootScope.$broadcast('$routeChangeError', nextRoute, lastRoute, error); 653 } 654 }); 655 } 656 } 657 658 function resolveLocals(route) { 659 if (route) { 660 var locals = angular.extend({}, route.resolve); 661 angular.forEach(locals, function(value, key) { 662 locals[key] = angular.isString(value) ? 663 $injector.get(value) : 664 $injector.invoke(value, null, null, key); 665 }); 666 var template = getTemplateFor(route); 667 if (angular.isDefined(template)) { 668 locals['$template'] = template; 669 } 670 return $q.all(locals); 671 } 672 } 673 674 675 function getTemplateFor(route) { 676 var template, templateUrl; 677 if (angular.isDefined(template = route.template)) { 678 if (angular.isFunction(template)) { 679 template = template(route.params); 680 } 681 } else if (angular.isDefined(templateUrl = route.templateUrl)) { 682 if (angular.isFunction(templateUrl)) { 683 templateUrl = templateUrl(route.params); 684 } 685 if (angular.isDefined(templateUrl)) { 686 route.loadedTemplateUrl = $sce.valueOf(templateUrl); 687 template = $templateRequest(templateUrl); 688 } 689 } 690 return template; 691 } 692 693 694 /** 695 * @returns {Object} the current active route, by matching it against the URL 696 */ 697 function parseRoute() { 698 // Match a route 699 var params, match; 700 angular.forEach(routes, function(route, path) { 701 if (!match && (params = switchRouteMatcher($location.path(), route))) { 702 match = inherit(route, { 703 params: angular.extend({}, $location.search(), params), 704 pathParams: params}); 705 match.$$route = route; 706 } 707 }); 708 // No route matched; fallback to "otherwise" route 709 return match || routes[null] && inherit(routes[null], {params: {}, pathParams:{}}); 710 } 711 712 /** 713 * @returns {string} interpolation of the redirect path with the parameters 714 */ 715 function interpolate(string, params) { 716 var result = []; 717 angular.forEach((string || '').split(':'), function(segment, i) { 718 if (i === 0) { 719 result.push(segment); 720 } else { 721 var segmentMatch = segment.match(/(\w+)(?:[?*])?(.*)/); 722 var key = segmentMatch[1]; 723 result.push(params[key]); 724 result.push(segmentMatch[2] || ''); 725 delete params[key]; 726 } 727 }); 728 return result.join(''); 729 } 730 }]; 731} 732 733ngRouteModule.provider('$routeParams', $RouteParamsProvider); 734 735 736/** 737 * @ngdoc service 738 * @name $routeParams 739 * @requires $route 740 * 741 * @description 742 * The `$routeParams` service allows you to retrieve the current set of route parameters. 743 * 744 * Requires the {@link ngRoute `ngRoute`} module to be installed. 745 * 746 * The route parameters are a combination of {@link ng.$location `$location`}'s 747 * {@link ng.$location#search `search()`} and {@link ng.$location#path `path()`}. 748 * The `path` parameters are extracted when the {@link ngRoute.$route `$route`} path is matched. 749 * 750 * In case of parameter name collision, `path` params take precedence over `search` params. 751 * 752 * The service guarantees that the identity of the `$routeParams` object will remain unchanged 753 * (but its properties will likely change) even when a route change occurs. 754 * 755 * Note that the `$routeParams` are only updated *after* a route change completes successfully. 756 * This means that you cannot rely on `$routeParams` being correct in route resolve functions. 757 * Instead you can use `$route.current.params` to access the new route's parameters. 758 * 759 * @example 760 * ```js 761 * // Given: 762 * // URL: http://server.com/index.html#/Chapter/1/Section/2?search=moby 763 * // Route: /Chapter/:chapterId/Section/:sectionId 764 * // 765 * // Then 766 * $routeParams ==> {chapterId:'1', sectionId:'2', search:'moby'} 767 * ``` 768 */
769function $RouteParamsProvider() { 770 this.$get = function() { return {}; }; 771} 772 773ngRouteModule.directive('ngView', ngViewFactory); 774ngRouteModule.directive('ngView', ngViewFillContentFactory); 775 776 777/** 778 * @ngdoc directive 779 * @name ngView 780 * @restrict ECA 781 * 782 * @description 783 * # Overview 784 * `ngView` is a directive that complements the {@link ngRoute.$route $route} service by 785 * including the rendered template of the current route into the main layout (`index.html`) file. 786 * Every time the current route changes, the included view changes with it according to the 787 * configuration of the `$route` service. 788 * 789 * Requires the {@link ngRoute `ngRoute`} module to be installed. 790 * 791 * @animations 792 * | Animation | Occurs | 793 * |----------------------------------|-------------------------------------| 794 * | {@link ng.$animate#enter enter} | when the new element is inserted to the DOM | 795 * | {@link ng.$animate#leave leave} | when the old element is removed from to the DOM | 796 * 797 * The enter and leave animation occur concurrently. 798 * 799 * @knownIssue If `ngView` is contained in an asynchronously loaded template (e.g. in another 800 * directive's templateUrl or in a template loaded using `ngInclude`), then you need to 801 * make sure that `$route` is instantiated in time to capture the initial 802 * `$locationChangeStart` event and load the appropriate view. One way to achieve this 803 * is to have it as a dependency in a `.run` block: 804 * `myModule.run(['$route', function() {}]);` 805 * 806 * @scope 807 * @priority 400 808 * @param {string=} onload Expression to evaluate whenever the view updates. 809 * 810 * @param {string=} autoscroll Whether `ngView` should call {@link ng.$anchorScroll 811 * $anchorScroll} to scroll the viewport after the view is updated. 812 * 813 * - If the attribute is not set, disable scrolling. 814 * - If the attribute is set without value, enable scrolling. 815 * - Otherwise enable scrolling only if the `autoscroll` attribute value evaluated 816 * as an expression yields a truthy value. 817 * @example 818 <example name="ngView-directive" module="ngViewExample" 819 deps="angular-route.js;angular-animate.js" 820 animations="true" fixBase="true"> 821 <file name="index.html"> 822 <div ng-controller="MainCtrl as main"> 823 Choose: 824 <a href="Book/Moby">Moby</a> | 825 <a href="Book/Moby/ch/1">Moby: Ch1</a> | 826 <a href="Book/Gatsby">Gatsby</a> | 827 <a href="Book/Gatsby/ch/4?key=value">Gatsby: Ch4</a> | 828 <a href="Book/Scarlet">Scarlet Letter</a><br/> 829 830 <div class="view-animate-container"> 831 <div ng-view class="view-animate"></div> 832 </div> 833 <hr /> 834 835 <pre>$location.path() = {{main.$location.path()}}</pre> 836 <pre>$route.current.templateUrl = {{main.$route.current.templateUrl}}</pre> 837 <pre>$route.current.params = {{main.$route.current.params}}</pre> 838 <pre>$routeParams = {{main.$routeParams}}</pre> 839 </div> 840 </file> 841 842 <file name="book.html"> 843 <div> 844 controller: {{book.name}}<br /> 845 Book Id: {{book.params.bookId}}<br /> 846 </div> 847 </file> 848 849 <file name="chapter.html"> 850 <div> 851 controller: {{chapter.name}}<br /> 852 Book Id: {{chapter.params.bookId}}<br /> 853 Chapter Id: {{chapter.params.chapterId}} 854 </div> 855 </file> 856 857 <file name="animations.css"> 858 .view-animate-container { 859 position:relative; 860 height:100px!important; 861 background:white; 862 border:1px solid black; 863 height:40px; 864 overflow:hidden; 865 } 866 867 .view-animate { 868 padding:10px; 869 } 870 871 .view-animate.ng-enter, .view-animate.ng-leave { 872 transition:all cubic-bezier(0.250, 0.460, 0.450, 0.940) 1.5s; 873 874 display:block; 875 width:100%; 876 border-left:1px solid black; 877 878 position:absolute; 879 top:0; 880 left:0; 881 right:0; 882 bottom:0; 883 padding:10px; 884 } 885 886 .view-animate.ng-enter { 887 left:100%; 888 } 889 .view-animate.ng-enter.ng-enter-active { 890 left:0; 891 } 892 .view-animate.ng-leave.ng-leave-active { 893 left:-100%; 894 } 895 </file> 896 897 <file name="script.js"> 898 angular.module('ngViewExample', ['ngRoute', 'ngAnimate']) 899 .config(['$routeProvider', '$locationProvider', 900 function($routeProvider, $locationProvider) { 901 $routeProvider 902 .when('/Book/:bookId', { 903 templateUrl: 'book.html', 904 controller: 'BookCtrl', 905 controllerAs: 'book' 906 }) 907 .when('/Book/:bookId/ch/:chapterId', { 908 templateUrl: 'chapter.html', 909 controller: 'ChapterCtrl', 910 controllerAs: 'chapter' 911 }); 912 913 $locationProvider.html5Mode(true); 914 }])
915 .controller('MainCtrl', ['$route', '$routeParams', '$location', 916 function($route, $routeParams, $location) { 917 this.$route = $route; 918 this.$location = $location; 919 this.$routeParams = $routeParams; 920 }]) 921 .controller('BookCtrl', ['$routeParams', function($routeParams) { 922 this.name = "BookCtrl"; 923 this.params = $routeParams; 924 }]) 925 .controller('ChapterCtrl', ['$routeParams', function($routeParams) { 926 this.name = "ChapterCtrl"; 927 this.params = $routeParams; 928 }]); 929 930 </file> 931 932 <file name="protractor.js" type="protractor"> 933 it('should load and compile correct template', function() { 934 element(by.linkText('Moby: Ch1')).click(); 935 var content = element(by.css('[ng-view]')).getText(); 936 expect(content).toMatch(/controller\: ChapterCtrl/); 937 expect(content).toMatch(/Book Id\: Moby/); 938 expect(content).toMatch(/Chapter Id\: 1/); 939 940 element(by.partialLinkText('Scarlet')).click(); 941 942 content = element(by.css('[ng-view]')).getText(); 943 expect(content).toMatch(/controller\: BookCtrl/); 944 expect(content).toMatch(/Book Id\: Scarlet/); 945 }); 946 </file> 947 </example> 948 */ 949 950 951/** 952 * @ngdoc event 953 * @name ngView#$viewContentLoaded 954 * @eventType emit on the current ngView scope 955 * @description 956 * Emitted every time the ngView content is reloaded. 957 */ 958ngViewFactory.$inject = ['$route', '$anchorScroll', '$animate']; 959function ngViewFactory($route, $anchorScroll, $animate) { 960 return { 961 restrict: 'ECA', 962 terminal: true, 963 priority: 400, 964 transclude: 'element', 965 link: function(scope, $element, attr, ctrl, $transclude) { 966 var currentScope, 967 currentElement, 968 previousLeaveAnimation, 969 autoScrollExp = attr.autoscroll, 970 onloadExp = attr.onload || ''; 971 972 scope.$on('$routeChangeSuccess', update); 973 update(); 974 975 function cleanupLastView() { 976 if (previousLeaveAnimation) { 977 $animate.cancel(previousLeaveAnimation); 978 previousLeaveAnimation = null; 979 } 980 981 if (currentScope) { 982 currentScope.$destroy(); 983 currentScope = null; 984 } 985 if (currentElement) { 986 previousLeaveAnimation = $animate.leave(currentElement); 987 previousLeaveAnimation.then(function() { 988 previousLeaveAnimation = null; 989 }); 990 currentElement = null; 991 } 992 } 993 994 function update() { 995 var locals = $route.current && $route.current.locals, 996 template = locals && locals.$template; 997 998 if (angular.isDefined(template)) { 999 var newScope = scope.$new(); 1000 var current = $route.current; 1001 1002 // Note: This will also link all children of ng-view that were contained in the original 1003 // html. If that content contains controllers, ... they could pollute/change the scope. 1004 // However, using ng-view on an element with additional content does not make sense... 1005 // Note: We can't remove them in the cloneAttchFn of $transclude as that 1006 // function is called before linking the content, which would apply child 1007 // directives to non existing elements. 1008 var clone = $transclude(newScope, function(clone) { 1009 $animate.enter(clone, null, currentElement || $element).then(function onNgViewEnter() { 1010 if (angular.isDefined(autoScrollExp) 1011 && (!autoScrollExp || scope.$eval(autoScrollExp))) { 1012 $anchorScroll(); 1013 } 1014 }); 1015 cleanupLastView(); 1016 }); 1017 1018 currentElement = clone; 1019 currentScope = current.scope = newScope; 1020 currentScope.$emit('$viewContentLoaded'); 1021 currentScope.$eval(onloadExp); 1022 } else { 1023 cleanupLastView(); 1024 } 1025 } 1026 } 1027 }; 1028} 1029 1030// This directive is called during the $transclude call of the first `ngView` directive. 1031// It will replace and compile the content of the element with the loaded template. 1032// We need this directive so that the element content is already filled when 1033// the link function of another directive on the same element as ngView 1034// is called. 1035ngViewFillContentFactory.$inject = ['$compile', '$controller', '$route']; 1036function ngViewFillContentFactory($compile, $controller, $route) { 1037 return { 1038 restrict: 'ECA', 1039 priority: -400, 1040 link: function(scope, $element) { 1041 var current = $route.current, 1042 locals = current.locals; 1043 1044 $element.html(locals.$template); 1045 1046 var link = $compile($element.contents()); 1047 1048 if (current.controller) { 1049 locals.$scope = scope; 1050 var controller = $controller(current.controller, locals); 1051 if (current.controllerAs) { 1052 scope[current.controllerAs] = controller; 1053 } 1054 $element.data('$ngControllerController', controller); 1055 $element.children().data('$ngControllerController', controller); 1056 } 1057 scope[current.resolveAs || '$resolve'] = locals; 1058 1059 link(scope); 1060 } 1061 }; 1062} 1063 1064 1065})(window, window.angular);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.