M-S M-S - 1 month ago 8
Javascript Question

Angular injector error with self invocation function syntax

I have created a basic angular app, with self invoking function

(function(){})


When ever I am running the application it is throwing injector error, if I remove the above code, it is working fine, I have gone through many sites but unable to fix this, and why it is happening I am unable to understand.

HTML:

<!DOCTYPE html>
<html>

<head>
<link rel="stylesheet" href="style.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script>
<script src="script.js"></script>
</head>

<body ng-app="myApp">
<h1>Hello Plunker!</h1>
</body>

</html>


script.js:

(function(){
angular.module("myApp", [])
});


Demo

Answer

You need to have like this,

(function(){
  angular.module("myApp", [])  
})();

DEMO