0

I have a simple controller which works absolutely fine.

var myApp = angular.module('myApp', []);
myApp.controller('ProductController', ['$scope', function ($scope) {
$scope.Message = "Hello World";
} ]);

However, if I want to put this inside a function, i.e.

(function () { 
var myApp = angular.module('myApp', []);
myApp.controller('ProductController', ['$scope', function ($scope) {
$scope.Message = "Hello World";
} ]);
});

I get the following error Error: ng:areq Bad Argument Argument 'ProductController' is not a function, got undefined

Html is like this

 <body ng-app="myApp">
<div ng-controller="ProductController">
  {{Message}}
</div>
</body>

1 Answer 1

2

your iife is not correct, you are missing invoking part of iife(Immediately-invoked function expression), you are not invoking it

(function () { 
var myApp = angular.module('myApp', []);
myApp.controller('ProductController', ['$scope', function ($scope) {
$scope.Message = "Hello World";
} ]);
})();

Working Demo:

(function () { 
var myApp = angular.module('myApp', []);
myApp.controller('ProductController', ['$scope', function ($scope) {
$scope.Message = "Hello World";
} ]);
})();
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myApp">
<div ng-controller="ProductController">
  {{Message}}
</div>
</body>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.