2016-11-26 61 views
2

在加載我的index.html我面對這個錯誤

的index.html

<html> 
<head>Testing 
<script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular.min.js" ></script> 
<script> 
var app = angular.module('myApp', []); 
app.controller('myCtrl', function($scope,$http) { 
    $scope.submit= function(){ 
var data = { 
    book: {    
    author: $scope.author, 
    title : $scope.title, 
    body : $scope.body 
    } 
} 

$http.post("/", data).success(function(data, status) { 
    console.log('Data posted successfully'); 
}) 
    } 
}); 
</script> 
</head> 

<body ng-app="myApp">  
    <div ng-controller="myCtrl"> 
    <form> 
     Author: 
     <input type="text" ng-model="author"> 
     <br> 
     <br> Title: 
     <input type="text" ng-model="title"> 
     <br> 
     <br> Body: 
     <input type="author" ng-model="body"> 
     <br> 
     <br> 
     <input type="submit" value="Submit" ng-click="submit()"> 
    </form> 
    </div> 
</body> 
</html> 

我運行它在我的node.js代碼是

server.js

var express = require('express'); 
var bodyParser = require('body-parser'); 
var app = express(); 
var path = require('path'); 


app.use(express.static(__dirname + './public')); 
app.use(express.static(path.join(__dirname, 'public'))); 
app.use(bodyParser.urlencoded({ extended: true })); 
app.use(bodyParser.json()); 


app.get('/', function(req, res){ 
    res.sendFile(__dirname +'/index.html'); 
}); 

app.post('/', function(req,res){ 
    console.log(req.body) 
    res.sendFile(__dirname +'/index1.html'); 
}); 

console.log('server running at 3000!') 
app.listen(3000); 

我得到正確的index.html,但在谷歌瀏覽器檢查它顯示我的錯誤

angular.js:38Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.5.6/ $injector/modulerr?p0=myApp&p1=Error%3A%2…ogleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.5.6%2Fangular.min.js%3A21%3A332)

+1

我認爲你必須加載JavaScript庫文件,請檢查 –

+0

嘿阿希什我應該加載javascript文件? –

+0

我已經包含angular.min.js @AshishPatel –

回答

1

您的代碼在我們的機器上工作正常。

正如你在一個評論,你總是讓舊文件說,應該是cache problem

如果是緩存問題,步驟清除緩存

1) Open Chrome. 
2) On your browser toolbar, tap More . 
3) Tap History, and then tap Clear browsing data. 
4) Under "Clear browsing data," select the checkboxes for Cookies and site data and Cached images and files. 
5) Use the menu at the top to select the amount of data that you want to delete. ... 
6) Tap Clear browsing data. 

如果你想從代碼中刪除緩存的視圖。

app.run(function($rootScope, $templateCache) { 
    $rootScope.$on('$routeChangeStart', function(event, next, current) { 
     if (typeof(current) !== 'undefined'){ 
      $templateCache.remove(current.templateUrl); 
     } 
    }); 
}); 

Here is a link where you get these sort of answers.

你也可以,

1) Open your browser console 
2) Go to network tab 
3) Refresh the page 
4) While refreshing, right click on network tab and click on, clear browser cookies, then press alt+f5 
+0

我試過所有的舊數據被刪除了我的密碼,所有的瀏覽器都加載了舊的文件 –

+0

舊的文件意味着什麼? 'html'還是'data'? – Sravan

+0

舊的html和js文件 –

1

相同的代碼工作在這裏

var app = angular.module('myApp', []); 
 
app.controller('myCtrl', function($scope, $http) { 
 
    $scope.submit = function() { 
 
    var data = { 
 
     book: { 
 
     author: $scope.author, 
 
     title: $scope.title, 
 
     body: $scope.body 
 
     } 
 
    } 
 

 
    $http.post("/", data).success(function(data, status) { 
 
     console.log('Data posted successfully'); 
 
    }) 
 
    } 
 
});
<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular.min.js"></script> 
 

 

 
    <script src="script.js"></script> 
 
</head> 
 

 

 
<body ng-app="myApp">  
 
    <div ng-controller="myCtrl"> 
 
    <form> 
 
     Author: 
 
     <input type="text" ng-model="author"> 
 
     <br> 
 
     <br> Title: 
 
     <input type="text" ng-model="title"> 
 
     <br> 
 
     <br> Body: 
 
     <input type="author" ng-model="body"> 
 
     <br> 
 
     <br> 
 
     <input type="submit" value="Submit" ng-click="submit()"> 
 
    </form> 
 
    </div> 
 
</body> 
 
    
 

 
</html>

+0

這個答案沒有意義,而應該在評論中添加plunkr,並要求OP提供更多輸入 –

+0

@sajeetharan這是否意味着我應該嘗試使用另一個名爲script.js的文件 –

+0

我試過了不行@sajeetharan –

1

後您的角度簡單地加入這行.js腳本行:

<script src="https://code.jquery.com/jquery-2.2.4.js"></script> 

它會工作..

+0

爲什麼jQuery在這裏? – Sajeetharan

相關問題