AngularJS bootstrap启动详解及实例代码
2016-09-14 15:22
806 查看
对于一般的使用者来说,AngularJS的ng-app都是手动绑定到某个dom元素。但是在一些应用中,这样就显得很不方便了。
绑定初始化
通过绑定来进行angular的初始化,会把js代码侵入到html中,但是对于新手使用来说,还是足够了!
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script src="http://apps.bdimg.com/libs/angular.js/1.2.16/angular.min.js"></script> </head> <body ng-app="myApp"> <div ng-controller="myCtrl"> {{ hello }} </div> <script type="text/javascript"> var myModule = angular.module("myApp",[]); myModule.controller("myCtrl",function($scope){ $scope.hello = "hello,angular!"; }); </script> </body> </html>
运行后,会显示hello,angular!
手动初始化
Angular中也提供了手动绑定的api――bootstrap,它的使用方式如下:
angular.bootstrap(element, [modules], [config]);
其中第一个参数element:是绑定ng-app的dom元素;
modules:绑定的模块名字
config:附加的配置
简单的看一下代码:
<html> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script src="http://apps.bdimg.com/libs/angular.js/1.2.16/angular.min.js"></script> <body id="body"> <div ng-controller="myCtrl"> {{ hello }} </div> <script type="text/javascript"> var app = angular.module("bootstrapTest",[]); app.controller("myCtrl",function($scope){ $scope.hello = "hello,angular from bootstrap"; }); // angular.bootstrap(document.getElementById("body"),['bootstrapTest']); angular.bootstrap(document,['bootstrapTest']); </script> </body> </html>
值得注意的是:
angular.bootstrap只会绑定第一次加载的对象。
后面重复的绑定或者其他对象的绑定,都会在控制台输出错误提示。
以上就是对AngularJS bootstrap 的资料整理,后续继续补充相关资料,谢谢大家对本站的支持!
您可能感兴趣的文章:
相关文章推荐
- AngularJS ng-bind-html 指令详解及实例代码
- angularjs+bootstrap实现自定义分页的实例代码
- AngularJs ng-route路由详解及实例代码
- angular使用bootstrap方法手动启动的实例代码
- angularJS Provider、factory、service详解及实例代码
- AngularJs bootstrap详解及示例代码
- AngularJs Modules详解及示例代码
- 实例代码详解jquery.slides.js
- AngularJs Scope详解及示例代码
- 第七篇Bootstrap表单布局实例代码详解(三种表单布局)
- AngularJS API之bootstrap启动
- AngularJS ng-change 指令的详解及简单实例
- Angular.js与Bootstrap相结合实现表格分页代码
- AngularJS equal比较对象实例详解
- JS 全屏和退出全屏详解及实例代码
- AngularJs directive详解及示例代码
- 基于AngularJs + Bootstrap + AngularStrap相结合实现省市区联动代码
- Angular.js与Bootstrap相结合实现手风琴菜单代码
- angular.js分页代码的实例
- AngularJS ng-app 指令实例详解