angularjs [ng:areq] Argument 'fn' is not a function, got string

I am new to angular js..I am getting follwing error please help me out

[ng:areq] Argument 'fn' is not a function, got string

var app = angular.module('demo',[]);


app.config('$routeProvider',function($routeProvider){

    $routeProvider.when('/add',{
        templateUrl:'demo/add.html',
        controller:'addcontroller'
    }).
    when('/order',{
        templateUrl:'demo/order.html',
        controller:'ordercontroller'
    });

});

app.controller('addcontroller',function($scope){
    $scope.message="order";
});
app.controller('ordercontroller',function($scope){
    $scope.message="order";
});


ANSWERS:


I think the error is in the config block, it should either be:

app.config(function($routeProvider){
  // routeProvider config
});

or better:

app.config(['$routeProvider', function($routeProvider){
  // routeProvider config, allows minification
}]);

the annotations are there for minification to work correctly. You can read more about it on AngularJS docs Please note that this practice needs to be done throughout the app to work correctly.


Although not directly related to the context of this question, this error message can also be caused by a resolve block returning something else than a function, like this:

$stateProvider.state('mystate', {
    url: "/myurl",
    templateUrl: "mytemplate.html",
    controller: "MyController",
    resolve: {
        authenticatedUser: securityAuthorizationProvider.requireAuthenticatedUser
    }
});

if securityAuthorizationProvider.requireAuthenticatedUser happens to be undefined, you can get this error.


I know this is an older post, but thought I'd contribute what was wrong with my code with this exact error. I was injecting a service into my controller this way:

theApp.directive('theDirective', 'myService', ['$http', function ($http, myService) {}]);

Instead of this:

theApp.directive('theDirective', ['$http', 'myService', function ($http, myService) {}]);

Notice that my service was included outside of the inline array annotation! It was a boneheaded move on my part that cost me way too much time.


In First line you need to use like this,

var app = angular.module('demo', ['ngRoute']);

and use the routing thing like this,

$routeProvider.when('/add').then({
        templateUrl:'demo/add.html',
        controller:'addcontroller'
    });

Just try these steps once.


I got this error by misunderstanding the way anonymous functions and named functions are treated in JavaScript.

This causes the error:

angular.module("app").factory("myDataService", ['$resource', myDataService]);
var myDataService = function($resource) {
    return $resource('/url');
}

I should've either done this:

var myDataService = function($resource) {
    return $resource('/url');
}
angular.module("app").factory("myDataService", ['$resource', myDataService]);

Or this:

angular.module("app").factory("myDataService", ['$resource', myDataService]);
function myDataService($resource) {
    return $resource('/url');
}

More on the difference between anonymous function and named function here


For the the problem was defining factory dependency outside the array. Most probably the minification was also an issue.

//Error in this
  angular
    .module('mymodule').factory('myFactory', 'thisconstant', function (thisconstant) {

});

This fixed by

//correct code
  angular
    .module('mymodule').factory('myFactory', ['thisconstant', function (thisconstant) {

}]);


 MORE:


 ? angularjs [ng:areq] Argument 'fn' is not a function, got string
 ? angularjs [ng:areq] Argument 'fn' is not a function, got string
 ? angularjs [ng:areq] Argument 'fn' is not a function, got string
 ? AngularJS Error: [ng:areq] Argument 'fn' is not a function, got string with Google Maps SDK Async Loader
 ? Error: [ng:areq] Argument 'xController' is not a function, got undefined
 ? AngularJs: Error: [ng:areq] Argument 'fn' is not a function, got undefined
 ? Error: [ng:areq] Argument 'meetupsController' is not a function, got undefined
 ? pass function name to function with JSON
 ? pass function name to function with JSON
 ? pass function name to function with JSON