[英]Unable to load angular controller file while running server side using nodejs
當控制器代碼寫入相同的html文件時,代碼有效。 但是,一旦我將控制器代碼分成myController.js文件並在src屬性中提到js文件,我就會收到$ injector模塊錯誤。 這是錯誤
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.
這是我的角度控制器
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;
});
};
}]);
我該如何克服呢? 作為,我想分離出所有js文件以進一步縮小。
具有角度代碼的完整html文件。
<!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>
希望您使用express.js啟用Web提供程序。
如果您使用express.js,請使用以下代碼提供靜態文件
app.use(express.static(__ dirname +'/ public'));
其中public是目錄,所有靜態文件均可用。
然后,請將包含角度模塊和控制器代碼的js文件放在公共目錄中,並將其包含在html文件中。
// [ route.js ]
app.get('/myController.js',function(req,res){
res.sendFile(__dirname + "/client/" + "myController.js");
});
添加到我的控制器的路由,這是缺少的部分。 因為,我是node的新手,所以我不知道“ script”標簽的“ src”中提到的文件也需要路由。 我認為node's只會使用文件協議來獲取這些文件。
Patrick的評論是一個更好的解決方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.