From 10646c9f6f62dd5130606d07ffe69770b3973f47 Mon Sep 17 00:00:00 2001 From: Igor Minar Date: Wed, 19 Jan 2011 14:50:29 -0800 Subject: add ng:view widget --- src/services.js | 2 +- src/widgets.js | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/services.js b/src/services.js index 3543b2ea..7742b174 100644 --- a/src/services.js +++ b/src/services.js @@ -616,7 +616,7 @@ function switchRouteMatcher(on, when, dstName) { * Watches $location.hashPath and tries to map the hash to an existing route * definition. It is used for deep-linking URLs to controllers and views (HTML partials). * - * $route is typically used in conjunction with ng:include widget. + * $route is typically used in conjunction with {@link angular.widget.ng:view ng:view} widget. * * @example
diff --git a/src/widgets.js b/src/widgets.js
index 3b0ffab7..4585629a 100644
--- a/src/widgets.js
+++ b/src/widgets.js
@@ -944,3 +944,81 @@ angularWidget("@ng:repeat", function(expression, element){
});
*/
angularWidget("@ng:non-bindable", noop);
+
+
+/**
+ * @ngdoc widget
+ * @name angular.widget.ng:view
+ *
+ * @description
+ * # Overview
+ * `ng:view` is a widget that complements the {@link angular.service.$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.
+ *
+ * This widget provides functionality similar to {@link angular.service.ng:include ng:include} when
+ * used like this:
+ *
+ *
+ + ++ */ +angularWidget('ng:view', function(element) { + var compiler = this; + + if (!element[0]['ng:compiled']) { + element[0]['ng:compiled'] = true; + return injectService(['$xhr.cache', '$route'], function($xhr, $route, element){ + $route.onChange(function(){ + var src, scope; + + if ($route.current) { + src = $route.current.template; + scope = $route.current.scope; + } + + if (src) { + $xhr('GET', src, function(code, response){ + element.html(response); + compiler.compile(element)(element, scope); + scope.$init(); + }); + } else { + element.html(''); + } + }); + }); + } else { + this.descend(true); + this.directives(true); + } +}); -- cgit v1.2.3