2012-03-08 23:00:38 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
2013-06-05 22:30:31 +00:00
|
|
|
ngRouteModule.directive('ngView', ngViewFactory);
|
|
|
|
|
|
2012-03-08 23:00:38 +00:00
|
|
|
/**
|
|
|
|
|
* @ngdoc directive
|
2013-06-05 22:30:31 +00:00
|
|
|
* @name ngRoute.directive:ngView
|
2012-03-08 23:00:38 +00:00
|
|
|
* @restrict ECA
|
|
|
|
|
*
|
|
|
|
|
* @description
|
|
|
|
|
* # Overview
|
2013-06-05 22:30:31 +00:00
|
|
|
* `ngView` is a directive that complements the {@link ngRoute.$route $route} service by
|
2012-03-08 23:00:38 +00:00
|
|
|
* 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.
|
|
|
|
|
*
|
2013-03-20 23:24:23 +00:00
|
|
|
* @animations
|
2013-07-12 00:58:23 +00:00
|
|
|
* 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.
|
2013-03-20 23:24:23 +00:00
|
|
|
*
|
2012-03-08 23:00:38 +00:00
|
|
|
* @scope
|
|
|
|
|
* @example
|
2013-06-05 22:30:31 +00:00
|
|
|
<example module="ngViewExample" deps="angular-route.js" animations="true">
|
2012-04-29 05:45:28 +00:00
|
|
|
<file name="index.html">
|
2012-06-06 22:54:40 +00:00
|
|
|
<div ng-controller="MainCntl as main">
|
2012-03-08 23:00:38 +00:00
|
|
|
Choose:
|
2012-04-12 04:12:48 +00:00
|
|
|
<a href="Book/Moby">Moby</a> |
|
|
|
|
|
<a href="Book/Moby/ch/1">Moby: Ch1</a> |
|
|
|
|
|
<a href="Book/Gatsby">Gatsby</a> |
|
|
|
|
|
<a href="Book/Gatsby/ch/4?key=value">Gatsby: Ch4</a> |
|
|
|
|
|
<a href="Book/Scarlet">Scarlet Letter</a><br/>
|
2012-03-08 23:00:38 +00:00
|
|
|
|
2013-07-12 00:58:23 +00:00
|
|
|
<div class="example-animate-container">
|
|
|
|
|
<div ng-view class="view-example"></div>
|
|
|
|
|
</div>
|
2012-03-08 23:00:38 +00:00
|
|
|
<hr />
|
|
|
|
|
|
2012-06-06 22:54:40 +00:00
|
|
|
<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>
|
2012-03-08 23:00:38 +00:00
|
|
|
</div>
|
2012-04-29 05:45:28 +00:00
|
|
|
</file>
|
|
|
|
|
|
|
|
|
|
<file name="book.html">
|
2013-04-02 23:41:16 +00:00
|
|
|
<div>
|
2012-06-06 22:54:40 +00:00
|
|
|
controller: {{book.name}}<br />
|
|
|
|
|
Book Id: {{book.params.bookId}}<br />
|
2013-04-02 23:41:16 +00:00
|
|
|
</div>
|
2012-04-29 05:45:28 +00:00
|
|
|
</file>
|
|
|
|
|
|
|
|
|
|
<file name="chapter.html">
|
2013-04-02 23:41:16 +00:00
|
|
|
<div>
|
2012-06-06 22:54:40 +00:00
|
|
|
controller: {{chapter.name}}<br />
|
|
|
|
|
Book Id: {{chapter.params.bookId}}<br />
|
|
|
|
|
Chapter Id: {{chapter.params.chapterId}}
|
2013-04-02 23:41:16 +00:00
|
|
|
</div>
|
|
|
|
|
</file>
|
|
|
|
|
|
|
|
|
|
<file name="animations.css">
|
2013-07-12 00:58:23 +00:00
|
|
|
.view-example {
|
2013-04-02 23:41:16 +00:00
|
|
|
-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;
|
|
|
|
|
-ms-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;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-12 00:58:23 +00:00
|
|
|
.example-animate-container {
|
|
|
|
|
position:relative;
|
|
|
|
|
height:100px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.view-example.ng-enter {
|
2013-04-02 23:41:16 +00:00
|
|
|
left:100%;
|
|
|
|
|
}
|
2013-07-12 00:58:23 +00:00
|
|
|
.view-example.ng-enter.ng-enter-active {
|
2013-04-02 23:41:16 +00:00
|
|
|
left:0;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-12 00:58:23 +00:00
|
|
|
.view-example.ng-leave { }
|
|
|
|
|
.view-example.ng-leave.ng-leave-active {
|
2013-04-02 23:41:16 +00:00
|
|
|
left:-100%;
|
|
|
|
|
}
|
2012-04-29 05:45:28 +00:00
|
|
|
</file>
|
|
|
|
|
|
|
|
|
|
<file name="script.js">
|
2013-06-05 22:30:31 +00:00
|
|
|
angular.module('ngViewExample', ['ngRoute'], function($routeProvider, $locationProvider) {
|
2012-04-29 05:45:28 +00:00
|
|
|
$routeProvider.when('/Book/:bookId', {
|
2012-05-17 23:36:18 +00:00
|
|
|
templateUrl: 'book.html',
|
2012-06-06 22:54:40 +00:00
|
|
|
controller: BookCntl,
|
2013-04-29 18:27:45 +00:00
|
|
|
controllerAs: 'book'
|
2012-04-29 05:45:28 +00:00
|
|
|
});
|
|
|
|
|
$routeProvider.when('/Book/:bookId/ch/:chapterId', {
|
2012-05-17 23:36:18 +00:00
|
|
|
templateUrl: 'chapter.html',
|
2012-06-06 22:54:40 +00:00
|
|
|
controller: ChapterCntl,
|
2013-04-29 18:27:45 +00:00
|
|
|
controllerAs: 'chapter'
|
2012-04-29 05:45:28 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// configure html5 to get links working on jsfiddle
|
|
|
|
|
$locationProvider.html5Mode(true);
|
|
|
|
|
});
|
|
|
|
|
|
2012-06-06 22:54:40 +00:00
|
|
|
function MainCntl($route, $routeParams, $location) {
|
|
|
|
|
this.$route = $route;
|
|
|
|
|
this.$location = $location;
|
|
|
|
|
this.$routeParams = $routeParams;
|
2012-04-29 05:45:28 +00:00
|
|
|
}
|
|
|
|
|
|
2012-06-06 22:54:40 +00:00
|
|
|
function BookCntl($routeParams) {
|
|
|
|
|
this.name = "BookCntl";
|
|
|
|
|
this.params = $routeParams;
|
2012-04-29 05:45:28 +00:00
|
|
|
}
|
|
|
|
|
|
2012-06-06 22:54:40 +00:00
|
|
|
function ChapterCntl($routeParams) {
|
|
|
|
|
this.name = "ChapterCntl";
|
|
|
|
|
this.params = $routeParams;
|
2012-04-29 05:45:28 +00:00
|
|
|
}
|
|
|
|
|
</file>
|
|
|
|
|
|
|
|
|
|
<file name="scenario.js">
|
2012-03-08 23:00:38 +00:00
|
|
|
it('should load and compile correct template', function() {
|
|
|
|
|
element('a:contains("Moby: Ch1")').click();
|
2012-03-09 08:00:05 +00:00
|
|
|
var content = element('.doc-example-live [ng-view]').text();
|
2012-03-08 23:00:38 +00:00
|
|
|
expect(content).toMatch(/controller\: ChapterCntl/);
|
|
|
|
|
expect(content).toMatch(/Book Id\: Moby/);
|
|
|
|
|
expect(content).toMatch(/Chapter Id\: 1/);
|
|
|
|
|
|
|
|
|
|
element('a:contains("Scarlet")').click();
|
2012-03-09 08:00:05 +00:00
|
|
|
content = element('.doc-example-live [ng-view]').text();
|
2012-03-08 23:00:38 +00:00
|
|
|
expect(content).toMatch(/controller\: BookCntl/);
|
|
|
|
|
expect(content).toMatch(/Book Id\: Scarlet/);
|
|
|
|
|
});
|
2012-04-29 05:45:28 +00:00
|
|
|
</file>
|
|
|
|
|
</example>
|
2012-03-08 23:00:38 +00:00
|
|
|
*/
|
2012-03-08 23:42:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @ngdoc event
|
2013-06-05 22:30:31 +00:00
|
|
|
* @name ngRoute.directive:ngView#$viewContentLoaded
|
|
|
|
|
* @eventOf ngRoute.directive:ngView
|
2012-04-06 23:35:17 +00:00
|
|
|
* @eventType emit on the current ngView scope
|
2012-03-08 23:42:35 +00:00
|
|
|
* @description
|
2012-04-06 23:35:17 +00:00
|
|
|
* Emitted every time the ngView content is reloaded.
|
2012-03-08 23:42:35 +00:00
|
|
|
*/
|
feat(ngAnimate): complete rewrite of animations
- ngAnimate directive is gone and was replaced with class based animations/transitions
- support for triggering animations on css class additions and removals
- done callback was added to all animation apis
- $animation and $animator where merged into a single $animate service with api:
- $animate.enter(element, parent, after, done);
- $animate.leave(element, done);
- $animate.move(element, parent, after, done);
- $animate.addClass(element, className, done);
- $animate.removeClass(element, className, done);
BREAKING CHANGE: too many things changed, we'll write up a separate doc with migration instructions
2013-06-18 17:59:57 +00:00
|
|
|
ngViewFactory.$inject = ['$route', '$anchorScroll', '$compile', '$controller', '$animate'];
|
|
|
|
|
function ngViewFactory( $route, $anchorScroll, $compile, $controller, $animate) {
|
2012-03-08 23:00:38 +00:00
|
|
|
return {
|
|
|
|
|
restrict: 'ECA',
|
|
|
|
|
terminal: true,
|
2013-07-12 00:58:23 +00:00
|
|
|
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;
|
|
|
|
|
}
|
2012-03-08 23:00:38 +00:00
|
|
|
}
|
2012-05-23 04:12:19 +00:00
|
|
|
|
2013-07-12 00:58:23 +00:00
|
|
|
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();
|
2012-05-23 04:12:19 +00:00
|
|
|
}
|
2012-03-08 23:00:38 +00:00
|
|
|
}
|
2012-03-30 22:03:20 +00:00
|
|
|
}
|
2012-03-08 23:00:38 +00:00
|
|
|
}
|
|
|
|
|
};
|
2013-06-05 22:30:31 +00:00
|
|
|
}
|