AngularJS使用ng-app自动加载bootstrap框架问题分析
本文实例分析了AngularJS使用ng-app自动加载bootstrap框架问题。分享给大家供大家参考,具体如下:
前面的文章《AngularJS框架的ng-app指令与自动加载实现方法分析》 提出了使用ng-app指令的情况。之前觉得出现第4和第5种情况很奇怪,因为只看到了现象,没有看到本质。JS错误,最直观的表现方式就是:弹出一个非常不友好的JS错误窗口。当脚本出现了未捕获的错误,浏览器才会弹出错误提示。还有一种比较隐晦的表示:在浏览器的控制台输出错误信息。这提示我们:使用JS框架的时候,一定要用下F12看看控制台是否有错误。
第3种情况 和 第5种情况:不带属性的ng-app模块不能自动加载,用F12发现控制台报错:
SCRIPT5022: [$injector:modulerr] Failed to instantiate module app1 due to:
Error: [$injector:nomod] Module 'app1' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument。
什么意思呢?大概就是我们使用了ng-app,要求自动加载我们自定义的模块,但是我们却没有提供这些模块。再看下官方的ngApp说明文档:
Use this directive to auto-bootstrap an AngularJS application. The ngApp directive designates the root element of the application and is typically placed near the root element of the page - e.g. on the <body> or <html> tags.Only one AngularJS application can be auto-bootstrapped per HTML document. The first ngApp found in the document will be used to define the root element to auto-bootstrap as an application。
1、如果在html中定义了ng-app,那么angular框架会自动初始化,不管它有没有带属性值;
如果没有使用ng-app,那么需要自己手动初始化。
2、如果ng-app没有属性值,angular会默认创建一个模块作为root,然后启动框架。
3、ng-app属性如果有值,即自定义module,也会被解析出来,前提是我们必须先创建module。
也就是说ng-app就是用来自动启动angular框架的,是否带属性值的差别在于:根模块的创建,到底是默认的,还是我们自定义的。
更多关于AngularJS相关内容感兴趣的读者可查看本站专题:《AngularJS入门与进阶教程》及《AngularJS MVC架构总结》
希望本文所述对大家AngularJS程序设计有所帮助。
相关文章
AngularJS解决ng界面长表达式(ui-set)的方法分析
这篇文章主要介绍了AngularJS解决ng界面长表达式(ui-set)的方法,通过具体问题的分析并结合实例形式给出了AngularJS长表达式的相关使用技巧,需要的朋友可以参考下2016-11-11
Angular4项目中添加i18n国际化插件ngx-translate的步骤详解
这篇文章主要跟大家介绍了关于Angular4项目中添加i18n国际化插件ngx-translate的步骤,文中介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面来一起看看吧。2017-07-07
angularJS Provider、factory、service详解及实例代码
这篇文章主要介绍了angularJS Provider详解及实例代码的相关资料,需要的朋友可以参考下2016-09-09
Angular中ng-options下拉数据默认值的设定方法
本篇文章主要介绍了Angular中ng-options下拉数据默认值的设定方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2017-06-06
ios设备中angularjs无法改变页面title的解决方法
今天小编就为大家分享一篇ios设备中angularjs无法改变页面title的解决方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2018-09-09


最新评论