Skip to content

Commit 9d5c533

Browse files
committed
ng:autobind now optionally takes element id
so it is possible to easily compile just a part of a document. e.g.: <html> <head> <title>partially compiled doc</title> <script src="angular.js" ng:autobind="compileThis"></script> </head> <body> this part won't be compiled: {{1+2}} <div id="compileThis" ng:init="i=0" ng:click="i = i+1"> Click count: {{i}} </div> </body> </html>
1 parent 7414e7b commit 9d5c533

File tree

4 files changed

+75
-28
lines changed

4 files changed

+75
-28
lines changed

src/Angular.js

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -836,19 +836,7 @@ function encodeUriSegment(val) {
836836
* This section explains how to bootstrap your application with angular, using either the angular
837837
* javascript file, or manually.
838838
*
839-
*
840-
* ## The angular distribution
841-
* Note that there are two versions of the angular javascript file that you can use:
842-
*
843-
* * `angular.js` - the development version - this file is unobfuscated, uncompressed, and thus
844-
* human-readable and useful when developing your angular applications.
845-
* * `angular.min.js` - the production version - this is a minified and obfuscated version of
846-
* `angular.js`. You want to use this version when you want to load a smaller but functionally
847-
* equivalent version of the code in your application. We use the Closure compiler to create this
848-
* file.
849-
*
850-
*
851-
* ## Auto-bootstrap with `ng:autobind`
839+
* # Auto-bootstrap with `ng:autobind`
852840
* The simplest way to get an <angular/> application up and running is by inserting a script tag in
853841
* your HTML file that bootstraps the `http://code.angularjs.org/angular-x.x.x.min.js` code and uses
854842
* the special `ng:autobind` attribute, like in this snippet of HTML:
@@ -866,9 +854,13 @@ function encodeUriSegment(val) {
866854
&lt;/html&gt;
867855
* </pre>
868856
*
869-
* The `ng:autobind` attribute tells <angular/> to compile and manage the whole HTML document. The
870-
* compilation occurs in the page's `onLoad` handler. Note that you don't need to explicitly add an
871-
* `onLoad` event; auto bind mode takes care of all the magic for you.
857+
* The `ng:autobind` attribute without any value tells angular to compile and manage the whole HTML
858+
* document. The compilation occurs as soon as the document is ready for DOM manipulation. Note that
859+
* you don't need to explicitly add an `onLoad` event handler; auto bind mode takes care of all the
860+
* work for you.
861+
*
862+
* In order to compile only a part of the document, specify the id of the element that should be
863+
* compiled as the value of the `ng:autobind` attribute, e.g. `ng:autobind="angularContent"`.
872864
*
873865
*
874866
* ## Auto-bootstrap with `#autobind`
@@ -893,6 +885,8 @@ function encodeUriSegment(val) {
893885
*
894886
* In this case it's the `#autobind` URL fragment that tells angular to auto-bootstrap.
895887
*
888+
* Similarly to `ng:autobind`, you can specify an element id that should be exclusively targeted for
889+
* compilation as the value of the `#autobind`, e.g. `#autobind=angularContent`.
896890
*
897891
* ## Filename Restrictions for Auto-bootstrap
898892
* In order for us to find the auto-bootstrap script attribute or URL fragment, the value of the
@@ -926,12 +920,9 @@ function encodeUriSegment(val) {
926920
&lt;script type="text/javascript" src="http://code.angularjs.org/angular-0.9.3.min.js"
927921
ng:autobind&gt;&lt;/script&gt;
928922
&lt;script type="text/javascript"&gt;
929-
(function(window, previousOnLoad){
930-
window.onload = function(){
931-
try { (previousOnLoad||angular.noop)(); } catch(e) {}
932-
angular.compile(window.document);
933-
};
934-
})(window, window.onload);
923+
(angular.element(document).ready(function() {
924+
angular.compile(document)();
925+
})(document);
935926
&lt;/script&gt;
936927
&lt;/head&gt;
937928
&lt;body&gt;
@@ -973,10 +964,12 @@ function encodeUriSegment(val) {
973964
* APIs are bound to fields of this global object.
974965
*
975966
*/
976-
function angularInit(config){
977-
if (config.autobind) {
978-
// TODO default to the source of angular.js
979-
var scope = compile(window.document)(createScope({'$config':config})),
967+
function angularInit(config, document){
968+
var autobind = config.autobind;
969+
970+
if (autobind) {
971+
var element = isString(autobind) ? document.getElementById(autobind) : document,
972+
scope = compile(element)(createScope({'$config':config})),
980973
$browser = scope.$service('$browser');
981974

982975
if (config.css)

src/angular-bootstrap.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@
153153
//angular-ie-compat.js needs to be pregenerated for development with IE<8
154154
if (msie<8) addScript('../angular-ie-compat.js');
155155

156-
angularInit(angularJsConfig(document));
156+
angularInit(angularJsConfig(document), document);
157157
}
158158

159159
if (window.addEventListener){

src/angular.suffix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
jqLiteWrap(document).ready(function(){
3-
angularInit(angularJsConfig(document));
3+
angularInit(angularJsConfig(document), document);
44
});
55

66
})(window, document);

test/AngularSpec.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,60 @@ describe('angular', function(){
356356
});
357357

358358

359+
describe('angularInit', function() {
360+
var dom;
361+
362+
beforeEach(function() {
363+
dom = jqLite('<div foo="{{1+2}}">{{2+3}}' +
364+
'<div id="child" bar="{{3+4}}">{{4+5}}</div>' +
365+
'</div>')[0];
366+
});
367+
368+
369+
afterEach(function() {
370+
dealoc(dom);
371+
});
372+
373+
374+
it('should not compile anything if autobind is missing or false', function() {
375+
angularInit({}, dom);
376+
expect(sortedHtml(dom)).toEqual('<div foo="{{1+2}}">{{2+3}}' +
377+
'<div bar="{{3+4}}" id="child">{{4+5}}</div>' +
378+
'</div>');
379+
});
380+
381+
382+
it('should compile the document if autobind is true', function() {
383+
angularInit({autobind: true}, dom);
384+
expect(sortedHtml(dom)).toEqual('<div foo="3" ng:bind-attr="{"foo":"{{1+2}}"}">' +
385+
'<span ng:bind="2+3">5</span>' +
386+
'<div bar="7" id="child" ng:bind-attr="{"bar":"{{3+4}}"}">'+
387+
'<span ng:bind="4+5">9</span>' +
388+
'</div>' +
389+
'</div>');
390+
});
391+
392+
393+
it('should compile only the element specified via autobind', function() {
394+
dom.getElementById = function() {
395+
return this.childNodes[1];
396+
}
397+
398+
angularInit({autobind: 'child'}, dom);
399+
expect(sortedHtml(dom)).toEqual('<div foo="{{1+2}}">{{2+3}}' +
400+
'<div bar="7" id="child" ng:bind-attr="{"bar":"{{3+4}}"}">'+
401+
'<span ng:bind="4+5">9</span>' +
402+
'</div>' +
403+
'</div>');
404+
});
405+
406+
407+
xit('should add custom css when specified via css', function() {
408+
//TODO
409+
});
410+
});
411+
412+
359413
describe('angular service', function() {
360414
it('should override services', function() {
361415
var scope = createScope();

0 commit comments

Comments
 (0)