'use strict'; ngRouteModule.directive('ngView', ngViewFactory); /** * @ngdoc directive * @name ngRoute.directive:ngView * @restrict ECA * * @description * # Overview * `ngView` is a directive that complements the {@link ngRoute.$route $route} service by * including the rendered template of the current route into the main layout (`index.html`) file. * Every time the current route changes, the included view changes with it according to the * configuration of the `$route` service. * * Requires the {@link ngRoute `ngRoute`} module to be installed. * * @animations * enter - animation is used to bring new content into the browser. * leave - animation is used to animate existing content away. * * The enter and leave animation occur concurrently. * * @scope * @example
Choose: Moby | Moby: Ch1 | Gatsby | Gatsby: Ch4 | Scarlet Letter

$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: {{book.name}}
Book Id: {{book.params.bookId}}
controller: {{chapter.name}}
Book Id: {{chapter.params.bookId}}
Chapter Id: {{chapter.params.chapterId}}
.example-animate-container { position:relative; background:white; border:1px solid black; height:40px; overflow:hidden; } .example-animate-container > div { padding:10px; } .view-example.ng-enter, .view-example.ng-leave { -webkit-transition:all cubic-bezier(0.250, 0.460, 0.450, 0.940) 1.5s; -moz-transition:all cubic-bezier(0.250, 0.460, 0.450, 0.940) 1.5s; -o-transition:all cubic-bezier(0.250, 0.460, 0.450, 0.940) 1.5s; transition:all cubic-bezier(0.250, 0.460, 0.450, 0.940) 1.5s; display:block; width:100%; border-left:1px solid black; position:absolute; top:0; left:0; right:0; bottom:0; padding:10px; } .example-animate-container { position:relative; height:100px; } .view-example.ng-enter { left:100%; } .view-example.ng-enter.ng-enter-active { left:0; } .view-example.ng-leave { } .view-example.ng-leave.ng-leave-active { left:-100%; } angular.module('ngViewExample', ['ngRoute', 'ngAnimate'], function($routeProvider, $locationProvider) { $routeProvider.when('/Book/:bookId', { templateUrl: 'book.html', controller: BookCntl, controllerAs: 'book' }); $routeProvider.when('/Book/:bookId/ch/:chapterId', { templateUrl: 'chapter.html', controller: ChapterCntl, controllerAs: 'chapter' }); // configure html5 to get links working on jsfiddle $locationProvider.html5Mode(true); }); function MainCntl($route, $routeParams, $location) { this.$route = $route; this.$location = $location; this.$routeParams = $routeParams; } function BookCntl($routeParams) { this.name = "BookCntl"; this.params = $routeParams; } function ChapterCntl($routeParams) { this.name = "ChapterCntl"; this.params = $routeParams; } it('should load and compile correct template', function() { element('a:contains("Moby: Ch1")').click(); var content = element('.doc-example-live [ng-view]').text(); expect(content).toMatch(/controller\: ChapterCntl/); expect(content).toMatch(/Book Id\: Moby/); expect(content).toMatch(/Chapter Id\: 1/); element('a:contains("Scarlet")').click(); content = element('.doc-example-live [ng-view]').text(); expect(content).toMatch(/controller\: BookCntl/); expect(content).toMatch(/Book Id\: Scarlet/); });
*/ /** * @ngdoc event * @name ngRoute.directive:ngView#$viewContentLoaded * @eventOf ngRoute.directive:ngView * @eventType emit on the current ngView scope * @description * Emitted every time the ngView content is reloaded. */ ngViewFactory.$inject = ['$route', '$anchorScroll', '$compile', '$controller', '$animate']; function ngViewFactory( $route, $anchorScroll, $compile, $controller, $animate) { return { restrict: 'ECA', terminal: true, transclude: 'element', compile: function(element, attr, linker) { return function(scope, $element, attr) { var currentScope, currentElement, onloadExp = attr.onload || ''; scope.$on('$routeChangeSuccess', update); update(); function cleanupLastView() { if (currentScope) { currentScope.$destroy(); currentScope = null; } if(currentElement) { $animate.leave(currentElement); currentElement = null; } } function update() { var locals = $route.current && $route.current.locals, template = locals && locals.$template; if (template) { var newScope = scope.$new(); linker(newScope, function(clone) { cleanupLastView(); clone.html(template); $animate.enter(clone, null, $element); var link = $compile(clone.contents()), current = $route.current; currentScope = current.scope = newScope; currentElement = clone; if (current.controller) { locals.$scope = currentScope; var controller = $controller(current.controller, locals); if (current.controllerAs) { currentScope[current.controllerAs] = controller; } clone.data('$ngControllerController', controller); clone.contents().data('$ngControllerController', controller); } link(currentScope); currentScope.$emit('$viewContentLoaded'); currentScope.$eval(onloadExp); // $anchorScroll might listen on event... $anchorScroll(); }); } else { cleanupLastView(); } } } } }; }