0
(function () {
    'use strict';

    function myController($scope, $http) {
        var vm = this;
        $scope.text = "Delhi";
    }

    myController.$inject = ["$scope", "$http"];
    angular
       .module("app")
       .controller("myController", myController);
})();

And My Html Code is here

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script src="angular.js"></script>
    <script src="app.js"></script>
</head>
<body ng-app="app">
    <div ng-controller="myController">
        <p>I am from {{text}}</p>
    </div>
</body>
</html>

But I my project is not working as expected. Gives below error

https://www.dropbox.com/s/itd5ryar56zqcxn/Screenshot%202016-08-06%2023.46.04.png?dl=0

angular.js:68 Uncaught Error: [$injector:nomod] Module 'app' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
http://errors.angularjs.org/1.5.5/$injector/nomod?p0=app
GOPAL SHARMA
  • 657
  • 2
  • 12
  • 37

1 Answers1

0

There are few issues with the code,

(i) You need to define the module initially and then inject the controller

(ii) You forgot to add the empty dependencies to the module '[]'

(function () {
    "use strict"; 
    angular
       .module("app",[])
       .controller("myController", myController);
    function myController($scope, $http) {
        var vm = this;
        $scope.text = "Delhi";
    }
    myController.$inject = ["$scope", "$http"];    
}());

Here is the working App

Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
  • 1
    I know it works fine like this, But I also have seen many controller working without [] . https://www.dropbox.com/s/bed9nwy5iacdvws/Screenshot%202016-08-07%2000.31.25.png?dl=0 – GOPAL SHARMA Aug 06 '16 at 19:01
  • @GopalSharma Yes its possible, but when you declare a module you need to define the dependencies, even with your example it should have been declared somewhere . Read more http://stackoverflow.com/questions/19957280/angularjs-best-practices-for-module-declaration – Sajeetharan Aug 06 '16 at 19:13