Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

when the controller code is written in the same html file, code works. But as soon as I separate the controller code into myController.js file and mention the js file in the src attribut I get an $injector module error. This is the error

Failed to load resource: the server responded with a status of 404 (Not Found)
angular.js:4547 Uncaught Error: [$injector:modulerr] Failed to instantiate module myApp due to:
Error: [$injector:nomod] Module 'myApp' 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.

This is my angular controller

var app = angular.module('myApp',[]);
app.controller('myCtrl',['$scope','$http', function($scope, $http) {

  $scope.submitForm=function(){ 

$http({
   method:"POST",
   url:"http://localhost:7000/frmAng/",
   headers : { 'Content-Type' : 'application/json'},
   data : {
       name : $scope.name,
       pwd : $scope.pwd
   }
}).then(function(response) {
      $scope.resFrmServer = response.data;
   },function(response){
      $scope.resFrmServer = response.statusText;
    });
  };  
}]);

How do I overcome this ? As, I want to separate out all js files for further minification.

Complete html file with angular code.

<!DOCTYPE html>
<html>
<head>
    <script src='https://ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular.js'></script>
</head>    
<body>



<div ng-app="myApp" ng-controller="myCtrl" align="center" > 

<hr>

<form >
    Name: <input type="text" ng-model="name"  required autofocus><br><br>
     Password:<input type="password" ng-model="pwd"><br><br> 
    <input type="submit" ng-click="submitForm()" text="Submit">
</form>
<h1>{{resFrmServer}}</h1>     
</div>
<script >
var app = angular.module('myApp',[]);
app.controller('myCtrl',['$scope','$http', function($scope, $http) {
  $scope.submitForm=function(){ 
$http({
   method:"POST",
   url:"http://localhost:7000/frmAng/",
   headers : { 'Content-Type' : 'application/json'},
   data : {
       name : $scope.name,
       pwd : $scope.pwd
   }
}).then(function(response) {
      $scope.resFrmServer = response.data;

   },function(response){
      $scope.resFrmServer = response.statusText;          
    });
  };  
}]);

</script>

share|improve this question
    
can you post html code? – Jay Mar 27 '16 at 11:14
1  
Fix your server (node) routing or path to script file. Showing us the angular code doesn't tell us why the 404 occurs – charlietfl Mar 27 '16 at 12:13
    
@Scarecrow, I've updated the post with the html code – Vickar Mar 28 '16 at 6:50
    
@charlietfl, there's no issue with the route, route works well with the above code. The issue is when I separate the angular code into a separate .js file – Vickar Mar 28 '16 at 6:56
up vote 0 down vote accepted

Hope you are using express.js for enabling web provider.

If you are using express.js, please use following code for supplying static files

app.use(express.static(__dirname + '/public'));

where public is the directory, where all static files are available.

Then please place your js file, which contains angular module and controller code, in public directory and include it from html file.

share|improve this answer
    
Thanks @Sagar, just updated my code with express.static middleware func. – Vickar Mar 29 '16 at 7:41
// [ route.js ]
app.get('/myController.js',function(req,res){

res.sendFile(__dirname + "/client/" + "myController.js");
});

adding a route to my controller, this was the missing part. As, I'm new to node, I wasn't aware that files mentioned in the "src" of the "script" tag too requires routes. I thought node'll simply use the file protocol to fetch those files.

Comment by Patrick is a better workaround.

share|improve this answer
    
If you're using express, you can solve this by using the express.static call, as explained in the docs – Patrick Mar 29 '16 at 7:21
    
Thanks @Patrick. That worked and also reduced my code. Thanks once again – Vickar Mar 29 '16 at 7:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.