AngularJs ReferenceError: angular is not defined
Asked Answered
T

9

16

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.

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

Tiebold answered 10/1, 2013 at 14:40 Comment(5)
have you referenced app/js/filters.js ?Toughen
do you mean include some angular js file or put the code inside of filter.js?Tiebold
"In order to create a new filter, you are going to create a phonecatFilters module and register your custom filter with this module:" docs.angularjs.org/tutorial/step_09Toughen
Are you sure you're loading the angular.js script? Can you provide a jsfiddle with an example?Halflife
Well, finally I realize the problem. This is a single page with legacy code. I was injecting angular in a ajax partial page. If you have the same problem, put the angular.js in the base html and do workround with the $scope. Cheers and tks to every one.Tiebold
T
5

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:

angular.bootstrap($('#your_div_loaded_in_ajax'),["myApp","other_module"]);
Tiebold answered 23/4, 2013 at 13:8 Comment(2)
what is "other_module"?Terminal
an example of calling other moduleTiebold
Q
47

You should put the include angular line first, before including any other js file

Queen answered 12/4, 2013 at 15:20 Comment(1)
@lborgav because Angular needs to load before it is utilized. Hence if you don't put it at the top, a reference error will occur.Siegbahn
D
10

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

The problem was solved if I had both angular.js and ui-grid referenced locally and as well declaring angular.js before ui-grid

<script src="lib/js/angularjs/1.4.3/angular.js"></script>
<script src="lib/js/ui-grid/3.0.7/ui-grid.js"></script>
Devisee answered 2/11, 2015 at 17:48 Comment(1)
I also found this to be my solution I use "angular.min.js" and "angular-route.min.js" from cdn code.angularjs.org/1.5.0 and ui-bootstrap-tpls.min.js from cdnjs.cloudflare.com, When using only local references it works as expectedSoyuz
T
5

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:

angular.bootstrap($('#your_div_loaded_in_ajax'),["myApp","other_module"]);
Tiebold answered 23/4, 2013 at 13:8 Comment(2)
what is "other_module"?Terminal
an example of calling other moduleTiebold
D
5

Always make sure that js file (angular.min.js) is referenced first in the HTML file. For example:

----------------- This reference will THROW error -------------------------

< script src="otherXYZ.js"></script>
< script src="angular.min.js"></script>

----------------- This reference will WORK as expected -------------------

< script src="angular.min.js"></script>
< script src="otherXYZ.js"></script>
Democratize answered 2/11, 2016 at 11:48 Comment(0)
F
2

As @bmleite already mentioned in the comments, you probably forgot to load 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

Flibbertigibbet answered 10/1, 2013 at 16:25 Comment(0)
V
2

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):

//= require angular-resource
Valois answered 25/5, 2013 at 2:22 Comment(0)
F
1

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.

Ferde answered 5/9, 2013 at 19:20 Comment(0)
V
0

I think this will happen if you'll use 'async defer' for (the file that contains the filter) while working with angularjs:

<script src="js/filter.js" type="text/javascript" async defer></script>

if you do, just remove 'async defer'.

Veranda answered 23/10, 2013 at 20:53 Comment(0)
B
0

If you've downloaded the angular.js file from Google, you need to make sure that Everyone has Read access to it, or it will not be loaded by your HTML file. By default, it seems to download with No access permissions, so you'll also be getting a message such as:

GET http://localhost/myApp/js/angular.js

This maddened me for about half an hour!

Boreas answered 20/2, 2016 at 12:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.