diff options
| author | Misko Hevery | 2011-11-10 20:04:15 -0800 |
|---|---|---|
| committer | Misko Hevery | 2011-11-14 20:31:18 -0800 |
| commit | 186a840cd34d3ffed7b351a1827e7736cd8d54c3 (patch) | |
| tree | 3f67580bd1526535439d19308abbe62e31666209 | |
| parent | b09595a3c12ba761772084b94767b635c5bbfaf2 (diff) | |
| download | angular.js-186a840cd34d3ffed7b351a1827e7736cd8d54c3.tar.bz2 | |
feat(bootstrap): added angular.bootstrap method
| -rw-r--r-- | docs/content/guide/dev_guide.bootstrap.manual_bootstrap.ngdoc | 2 | ||||
| -rw-r--r-- | src/Angular.js | 34 | ||||
| -rw-r--r-- | test/AngularSpec.js | 11 |
3 files changed, 37 insertions, 10 deletions
diff --git a/docs/content/guide/dev_guide.bootstrap.manual_bootstrap.ngdoc b/docs/content/guide/dev_guide.bootstrap.manual_bootstrap.ngdoc index 1adbb0d2..8ff74d91 100644 --- a/docs/content/guide/dev_guide.bootstrap.manual_bootstrap.ngdoc +++ b/docs/content/guide/dev_guide.bootstrap.manual_bootstrap.ngdoc @@ -17,7 +17,7 @@ explicitly. <script src="http://code.angularjs.org/angular.js"></script> <script> angular.element(document).ready(function() { - angular.compile(document)().$apply(); + angular.bootstrap(document); }); </script> </head> diff --git a/src/Angular.js b/src/Angular.js index 66b592f2..096f5b53 100644 --- a/src/Angular.js +++ b/src/Angular.js @@ -819,29 +819,44 @@ function encodeUriQuery(val, pctEncodeSpaces) { * `ng:autobind="[root element ID]"` tells Angular to compile and manage part of the document, * starting at "root element ID". * - */ function angularInit(config, document){ var autobind = config.autobind; if (autobind) { - var modules = [ngModule]; + var modules = []; forEach((config.modules || '').split(','), function(module){ module = trim(module); if (module) { modules.push(module); } }); - createInjector(modules, angularModule)(['$rootScope', '$compile', '$injector', function(scope, compile, injector){ - scope.$apply(function(){ - var element = jqLite(isString(autobind) ? document.getElementById(autobind) : document); - element.data('$injector', injector); - compile(element)(scope); - }); - }]); + bootstrap(jqLite(isString(autobind) ? document.getElementById(autobind) : document), modules); } } +/** + * @ngdoc function + * @name angular.bootstrap + * @description + * Use this function to manually start up angular application. + * + * See: {@link guide/dev_guide.bootstrap.manual_bootstrap Bootstrap} + * + * @param {Element} element DOM element which is the root of angular application. + * @param {Array<String,function>=} modules an array of module declarations. See: {@link angular.module modules} + */ +function bootstrap(element, modules) { + modules = modules || []; + modules.unshift(ngModule); + createInjector(modules, angularModule)(['$rootScope', '$compile', '$injector', function(scope, compile, injector){ + scope.$apply(function() { + element.data('$injector', injector); + compile(element)(scope); + }); + }]); +} + function angularJsConfig(document) { bindJQuery(); var scripts = document.getElementsByTagName('script'), @@ -903,6 +918,7 @@ function assertArgFn(arg, name) { function publishExternalAPI(angular){ extend(angular, { + 'bootstrap': bootstrap, 'copy': copy, 'extend': extend, 'equals': equals, diff --git a/test/AngularSpec.js b/test/AngularSpec.js index 6229120d..e14f2e27 100644 --- a/test/AngularSpec.js +++ b/test/AngularSpec.js @@ -529,4 +529,15 @@ describe('angular', function() { expect(version.codeName).toBe('"NG_VERSION_CODENAME"'); }); }); + + describe('bootstrap', function() { + it('should bootstrap app', function(){ + var element = jqLite('<div>{{1+2}}</div>'); + var injector; + angular.bootstrap(element, [function($injector){ injector = $injector; }]); + expect(injector).toBeDefined(); + expect(element.data('$injector')).toBe(injector); + dealoc(element); + }); + }); }); |
