Javascript AngularJs ReferenceError:角度未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14260399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AngularJs ReferenceError: angular is not defined
提问by FAvIo41
I trying to add a custom filter, but if I use the following code:
我尝试添加自定义过滤器,但如果我使用以下代码:
angular.module('myApp',[]).filter('startFrom', function() {
return function(input, start) {
start = +start; //parse to int
return input.slice(start);
}
});
But if I do so, I get: "ReferenceError: angular is not defined" in firebug.
但是如果我这样做,我会在萤火虫中得到:“ReferenceError: angular is not defined”。
The rest of application is working fine, I am using ng-app in a tag div not in tag html, and https://ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular.js
应用程序的其余部分工作正常,我在标签 div 中而不是在标签 html 中使用 ng-app,并且https://ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular.js
采纳答案by FAvIo41
Well in the way this is a single page application, the solution used was:
好吧,这是一个单页应用程序,使用的解决方案是:
load the content with AJAX, like any other controller, and then call:
像任何其他控制器一样使用 AJAX 加载内容,然后调用:
angular.bootstrap($('#your_div_loaded_in_ajax'),["myApp","other_module"]);
回答by V? Ng?
You should put the include angular line first, before including any other js file
在包含任何其他 js 文件之前,您应该首先放置包含角线
回答by nsk
I faced similar issue due to local vs remote referencing
由于本地引用和远程引用,我遇到了类似的问题
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.js"></script>
<script src="lib/js/ui-grid/3.0.7/ui-grid.js"></script>
As ui-grid.js is dependent on angular.js, ui-grid requires angular.js by the time its loaded. But in the above case, ui-grid.js [locally saved reference file] is loaded before angularjs was loaded [from internet],
由于 ui-grid.js 依赖于 angular.js,因此 ui-grid 在加载时需要 angular.js。但是在上面的例子中,ui-grid.js [本地保存的参考文件] 在 angularjs 被加载之前被加载 [来自互联网],
The problem was solved if I had both angular.js and ui-grid referenced locally and as well declaring angular.js before ui-grid
如果我在本地同时引用了 angular.js 和 ui-grid 并且在 ui-grid 之前声明了 angular.js,那么问题就解决了
<script src="lib/js/angularjs/1.4.3/angular.js"></script>
<script src="lib/js/ui-grid/3.0.7/ui-grid.js"></script>
回答by NagarajKaundinya
Always make sure that js file (angular.min.js) is referenced first in the HTML file. For example:
始终确保在 HTML 文件中首先引用 js 文件 (angular.min.js)。例如:
----------------- This reference will THROWerror -------------------------
-----------------这个节点将THROW错误-------------------------
< script src="otherXYZ.js"></script>
< script src="angular.min.js"></script>
----------------- This reference will WORKas expected -------------------
----------------- 此参考将按预期工作-------------------
< script src="angular.min.js"></script>
< script src="otherXYZ.js"></script>
回答by Timo
In case you'd happen to be using rails and the angular-rails gem then the problem is easily corrected by adding this missing line to application.js (or what ever is applicable in your situation):
如果您碰巧使用了 rails 和 angular-rails gem,那么可以通过将此缺少的行添加到 application.js(或适用于您的情况的任何内容)来轻松纠正问题:
//= require angular-resource
回答by Mark Rajcok
As @bmleite already mentioned in the comments, you probably forgot to load angular.js.
正如@bmleite 在评论中已经提到的那样,您可能忘记加载 angular.js。
If I create a fiddle with angular directives in it, but don't include angular.js I get the exact same error in the Chrome console: Uncaught ReferenceError: angular is not defined
如果我创建一个带有 angular 指令的小提琴,但不包含 angular.js,我会在 Chrome 控制台中得到完全相同的错误: Uncaught ReferenceError: angular is not defined
回答by Michael Bushe
I ran into this because I made a copy-and-paste of ngBoilerplate into my project on a Mac without Finder showing hidden files. So .bower was not copied with the rest of ngBoilerplate. Thus bower moved resources to bower_components (defult) instead of vendor (as configured) and my app didn't get angular. Probably a corner case, but it might help someone here.
我遇到了这个问题,因为我在 Mac 上将 ngBoilerplate 复制并粘贴到我的项目中,而 Finder 没有显示隐藏文件。所以 .bower 没有与 ngBoilerplate 的其余部分一起复制。因此,bower 将资源移动到 bower_components(默认)而不是供应商(按配置),我的应用程序没有变得有角度。可能是一个角落案例,但它可能会帮助这里的人。
回答by Elad Amsalem
I think this will happen if you'll use 'async defer' for (the file that contains the filter) while working with angularjs:
我认为如果您在使用 angularjs 时将“async defer”用于(包含过滤器的文件),就会发生这种情况:
<script src="js/filter.js" type="text/javascript" async defer></script>
if you do, just remove 'async defer'.
如果这样做,只需删除“异步延迟”。
回答by Chris
If you've downloaded the angular.js file from Google, you need to make sure that Everyone has Read accessto it, or it will not be loaded by your HTML file. By default, it seems to download with No accesspermissions, so you'll also be getting a message such as:
如果您已从 Google 下载了 angular.js 文件,则需要确保每个人都具有读取权限,否则您的 HTML 文件将不会加载该文件。默认情况下,它似乎在没有访问权限的情况下下载,因此您还会收到一条消息,例如:
This maddened me for about half an hour!
这让我疯狂了大约半小时!

