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>