From ef4bb28be13e99f96c9ace5936cf26a174a0e5f0 Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Sat, 12 Feb 2011 10:13:28 -0800 Subject: Change API angular.compile(element)([scope], [element/true]) --- src/Angular.js | 51 ++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 40 insertions(+), 11 deletions(-) (limited to 'src/Angular.js') diff --git a/src/Angular.js b/src/Angular.js index 9b2c7ea6..9eaeb093 100644 --- a/src/Angular.js +++ b/src/Angular.js @@ -793,21 +793,50 @@ function merge(src, dst) { * @function * * @description - * Compiles a piece of HTML or DOM into a {@link angular.scope scope} object. + * Compiles a piece of HTML string or DOM into a view and produces a linking function, which can + * then be used to link {@link angular.scope scope} and the template together. The compilation + * process walks the DOM tree and tries to match DOM elements to {@link angular.markup markup}, + * {@link angular.attrMarkup attrMarkup}, {@link angular.widget widgets}, and + * {@link angular.directive directives}. For each match it executes coresponding markup, \ + * attrMarkup, widget or directive template function and collects the instance functions into a + * single linking function which is then returned. The linking function can then be used + * many-times-over on clones of compiled DOM structure, (For example when compiling + * {@link angular.widget.@ng:repeat repeater} the resulting linking function is called once for + * each item in the collection. The `ng:repeat` does this by cloning the template DOM once for + * each item in collection and then calling the linking function to link the cloned template + * with the a new scope for each item in the collection.) + *
- var scope1 = angular.compile(window.document);
+ var mvc1 = angular.compile(window.document)();
+ mvc1.view; // compiled view elment
+ mvc1.scope; // scope bound to the element
- var scope2 = angular.compile('click me');
+ var mvc2 = angular.compile('click me')();
*
- * @param {string|DOMElement} element Element to compile.
- * @param {Object=} parentScope Scope to become the parent scope of the newly compiled scope.
- * @returns {Object} Compiled scope object.
+ * @param {string|DOMElement} element Element or HTML to compile into a template function.
+ * @returns {function([scope][, element])} a template function which is used to bind element
+ * and scope. Where:
+ *
+ * * `scope` - {@link angular.scope scope} A scope to bind to. If none specified, then a new
+ * root scope is created.
+ * * `element` - {@link angular.element element} Element to use as the template. If none
+ * specified then reuse the element from `angular.compile(element)`. If `true`
+ * then clone the `angular.compile(element)`. The element must be either the same
+ * element as `angular.compile(element)` or an identical clone to
+ * `angular.compile(element)`. Using an element with differnt structure will cause
+ * unpredictable behavior.
+ *
+ * Calling the template function returns object: `{scope:?, view:?}`, where:
+ *
+ * * `view` - the DOM element which represents the compiled template. Either same or clone of
+ * `element` specifed in compile or template function.
+ * * `scope` - scope to which the element is bound to. Either a root scope or scope specified
+ * in the template function.
*/
-function compile(element, parentScope) {
- var compiler = new Compiler(angularTextMarkup, angularAttrMarkup, angularDirective, angularWidget),
- $element = jqLite(element);
- return compiler.compile($element)($element, parentScope);
+function compile(element) {
+ return new Compiler(angularTextMarkup, angularAttrMarkup, angularDirective, angularWidget)
+ .compile(element);
}
/////////////////////////////////////////////////
@@ -989,7 +1018,7 @@ function toKeyValue(obj) {
function angularInit(config){
if (config.autobind) {
// TODO default to the source of angular.js
- var scope = compile(window.document, _null, {'$config':config}),
+ var scope = compile(window.document)(null, createScope({'$config':config})),
$browser = scope.$service('$browser');
if (config.css)
--
cgit v1.2.3