How do we load AngularJS at the end of body instead of the header?
默认情况下,除非我将其放在<head>中,否则AngularJS似乎无法正常工作。有没有办法将其放在<body>的末尾?
我的代码在页脚中看起来像这样:
1 2 3 4 5 6 7 8 9
| $(document).ready(function() {
var myApp = angular.module("myApp", []);
myApp.bootstrap(document, ["myApp"]);
myApp.controller("AppController",["$scope","$http", function() {
// so stuff with the $scope.
}]);
}); |
编辑3/31/14:
根据ederollora的回答并进行了一些研究,我发现在定义所有内容之后,都需要调用angular.bootstrap()的调用。上面的代码变为:
1 2 3 4 5 6 7 8 9
| $(document).ready(function() {
var myApp = angular.module("myApp", []);
myApp.controller("AppController",["$scope","$http", function() {
// so stuff with the $scope.
}]);
myApp.bootstrap(document, ["myApp"]); // compile the app last
}); |
此外,为了将我的应用程序从jQuery迁移到Angular,我将document.ready调用更改为有角版本:
1 2 3 4 5 6 7 8 9
| angular.element(document).ready(function() {
var myApp = angular.module("myApp", []);
myApp.controller("AppController",["$scope","$http", function() {
// so stuff with the $scope.
}]);
myApp.bootstrap(document, ["myApp"]); // compile the app last
}); |
在文档中并不确定在定义所有内容后必须调用angular.boostrap(),所以我去了并改进了文档。
- "默认情况下,除非我将其放在<head>中,否则AngularJS似乎无法工作。"这是什么意思? script标签?在body中可以正常工作。示例:jsbin.com/zoyedeba/1(来源:jsbin.com/zoyedeba/1/edit)。
-
AngularJS不需要放在HEAD中...
-
嗯...。我需要找出为什么<body>末尾它对我不起作用。
-
一直以来的问题是,在定义控制器和指令之前,我一直在页脚中调用angular.bootstrap(),这是行不通的。定义完所有内容后必须调用bootstrap()。 bootstrap的AngularJS文档中缺少此信息,我在以下请求请求中添加了它:github.com/angular/angular.js/pull/6921
-
我更新了我的问题。
来自AngularJS文档页面:
Angular Tag
This example shows the recommended path for integrating Angular with what we call automatic initialization.
1 2 3 4 5 6 7
| <!doctype html>
<html xmlns:ng="http://angularjs.org" ng-app>
<body>
...
<script src="angular.js">
</body>
</html> |
Place the script tag at the bottom of the page. Placing script tags at the end of the page improves app load time because the HTML loading is not blocked by loading of the angular.js script. You can get the latest bits from http://code.angularjs.org. Please don't link your production code to this URL, as it will expose a security hole on your site. For experimental development linking to our site is fine.
找到信息:在这里
- 谢谢!当我找出为什么<body>末尾对我来说不起作用时,我会回发。
-
是的,请修改我的答案,使其更适合您的问题。
-
您正在使用哪个浏览器?
-
在此链接中,您具有有关手动初始化的信息:docs.angularjs.org/guide/bootstrap#manual-initialization。在手动初始化之前,有一些关于自动初始化的信息。阅读这两个主题应该给您一些建议
-
啊啊。我发现了问题。只要它不在$(document).ready(function() {});内,它就可以在页脚中正常工作。知道为什么它不能在jQuery \\的document.ready函数中使用吗?
-
哦我是否需要在document.ready中进行相同的初始化?
-
据我所知,初始化不应该放在javascript中。您应该遵循我在有关"手动初始化"的链接的答案中写给您的示例中的一个
-
不,你不需要。如果您希望在初始化时进行精确控制,那么可以,您应该添加一些JavaScript代码。但是,如果您可以自动初始化,则可以使用自动初始化。一旦找到DOM加载(称为DOMContentLoaded的DOM加载事件)或脚本标签,它就会初始化。因此,将脚本标记<script src="http://code.angularjs.org/angular.js">放在</body>之前应该可以
-
谢谢!是的,我可以自动初始化,但是尝试手动初始化是行不通的。我正在尝试:hastebin.com/qahuqikogi.xml
-
您是否尝试过文档中手动初始化状态的代码?
-
我看到了链接,第一个/path/to/angular.js只是一个示例,还是实际的代码?
-
那只是一个例子...但是您可以假设它是实际的angular.js库。当我将其设为自动时,一切工作正常,但是如果将其package在angular.element(document).ready(function() {...});中并添加angular.bootstrap()行,它将无法正常工作。我正在做演示。这是我逐字记录的代码。这有效:hastebin.com/disecikegi.xml而且不起作用:hastebin.com/xugavupubo.xml
-
我无法进行手动初始化,因此无法终生使用。这是我的整个代码:hastebin.com/xoxoniyaja.xml这是它给我的AngularJS错误:bit.ly/1dIuMY1好像我在手动初始化文档中一样,但是我有更多的代码。
-
好吧,我绝对不是该领域的专家,但是您可以粘贴(或任何其他)您个人所遭受的异常跟踪吗?
-
但是,如果您仍然遇到手动初始化问题,那么我就不必太担心,因为angularJS在加载DOM或到达脚本标记后就足够智能地加载脚本。然后它可以比手动优化。
-
我想到了!设置控制器(在本例中为" RootController ")之后,我不得不将调用移至angular.bootstrap()到某个位置。因此,我想angular.bootstrap会编译应用程序,该程序依赖于模块,指令等在编译发生之前已经进行了全部配置的应用程序。
-
好,请您编辑我的答案,以便添加您的确切问题解决方案以使其更加完整。我们可以帮助更多的人!
-
因此,"入门"示例中甚至没有使用"推荐方式"? code.angularjs.org/0.10.6/docs-0.10.6/misc/started
-
开发人员指南:docs.angularjs.org/guide/bootstrap#angular-script-tag。也许开始就意味着那个。