Shubham Khatri Shubham Khatri - 4 months ago 44
AngularJS Question

AngularJS Error: ng:areq Bad Argument while using controller

I am a beginner with AngularJs and I am trying to use controller while creating a simple application. However I am getting an Error and I can't quite figure it out


Error: ng:areq Bad Argument. Argument 'languages' is not a function, got undefined


<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
<title>Angular JS | Controllers</title>
<script type="text/javascript" src="../resources/js/angular.min.js"></script>

<script>
(function(angular){
var myApp = angular.module('myApp', []);
myApp.controller = ('languages', function($scope){
$scope.myFavLanguage = 'None';
});
})(window.angular);


</script>
</head>
<body >

<div ng-controller="languages">
Select your favourite langauge:
<button>ReactJS</button>
<button>PHP</button>
<button>JavaScript</button>
<button>C++</button>

<p>Your favourite language is {{myFavLanguage}}</p>
</div>


</body>
</html>


I have searched over the internet and gone through a few questions on stackoverflow and could'nt get any of them to solve my problem.
These are the ones that I have visited along with a few others. Kindly visit these before marking it as duplicate because it isn't:

Angularjs bad argument ng:areq error

Angularjs: Error: [ng:areq] Argument 'HomeController' is not a function, got undefined

Argument ... is not a function, got undefined

Thanks for any help

Answer

To define a controller, use the controller() method.

myApp.controller = ('languages', function($scope){
                 ^

Remove the = from this.

myApp.controller('languages', function($scope) {

Check Docs for more information.

I'll suggest you to use min-safe syntax.

myApp.controller('languages', [$scope, function ('$scope') {

}]);
Comments