feat(bootstrap): added angular.bootstrap method

This commit is contained in:
Misko Hevery 2011-11-10 20:04:15 -08:00
parent b09595a3c1
commit 186a840cd3
3 changed files with 37 additions and 10 deletions

View file

@ -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>

View file

@ -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,

View file

@ -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);
});
});
});