Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 186a840

Browse files
committed
feat(bootstrap): added angular.bootstrap method
1 parent b09595a commit 186a840

File tree

3 files changed

+37
-10
lines changed

3 files changed

+37
-10
lines changed

docs/content/guide/dev_guide.bootstrap.manual_bootstrap.ngdoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ explicitly.
1717
<script src="http://code.angularjs.org/angular.js"></script>
1818
<script>
1919
angular.element(document).ready(function() {
20-
angular.compile(document)().$apply();
20+
angular.bootstrap(document);
2121
});
2222
</script>
2323
</head>

src/Angular.js

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -819,29 +819,44 @@ function encodeUriQuery(val, pctEncodeSpaces) {
819819
* `ng:autobind="[root element ID]"` tells Angular to compile and manage part of the document,
820820
* starting at "root element ID".
821821
*
822-
823822
*/
824823
function angularInit(config, document){
825824
var autobind = config.autobind;
826825

827826
if (autobind) {
828-
var modules = [ngModule];
827+
var modules = [];
829828
forEach((config.modules || '').split(','), function(module){
830829
module = trim(module);
831830
if (module) {
832831
modules.push(module);
833832
}
834833
});
835-
createInjector(modules, angularModule)(['$rootScope', '$compile', '$injector', function(scope, compile, injector){
836-
scope.$apply(function(){
837-
var element = jqLite(isString(autobind) ? document.getElementById(autobind) : document);
838-
element.data('$injector', injector);
839-
compile(element)(scope);
840-
});
841-
}]);
834+
bootstrap(jqLite(isString(autobind) ? document.getElementById(autobind) : document), modules);
842835
}
843836
}
844837

838+
/**
839+
* @ngdoc function
840+
* @name angular.bootstrap
841+
* @description
842+
* Use this function to manually start up angular application.
843+
*
844+
* See: {@link guide/dev_guide.bootstrap.manual_bootstrap Bootstrap}
845+
*
846+
* @param {Element} element DOM element which is the root of angular application.
847+
* @param {Array<String,function>=} modules an array of module declarations. See: {@link angular.module modules}
848+
*/
849+
function bootstrap(element, modules) {
850+
modules = modules || [];
851+
modules.unshift(ngModule);
852+
createInjector(modules, angularModule)(['$rootScope', '$compile', '$injector', function(scope, compile, injector){
853+
scope.$apply(function() {
854+
element.data('$injector', injector);
855+
compile(element)(scope);
856+
});
857+
}]);
858+
}
859+
845860
function angularJsConfig(document) {
846861
bindJQuery();
847862
var scripts = document.getElementsByTagName('script'),
@@ -903,6 +918,7 @@ function assertArgFn(arg, name) {
903918

904919
function publishExternalAPI(angular){
905920
extend(angular, {
921+
'bootstrap': bootstrap,
906922
'copy': copy,
907923
'extend': extend,
908924
'equals': equals,

test/AngularSpec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,4 +529,15 @@ describe('angular', function() {
529529
expect(version.codeName).toBe('"NG_VERSION_CODENAME"');
530530
});
531531
});
532+
533+
describe('bootstrap', function() {
534+
it('should bootstrap app', function(){
535+
var element = jqLite('<div>{{1+2}}</div>');
536+
var injector;
537+
angular.bootstrap(element, [function($injector){ injector = $injector; }]);
538+
expect(injector).toBeDefined();
539+
expect(element.data('$injector')).toBe(injector);
540+
dealoc(element);
541+
});
542+
});
532543
});

0 commit comments

Comments
 (0)