2016-11-30 124 views
0

我正在瀏覽w3schools的教程,我複製了他們的片段之一。當我嘗試在瀏覽器中查看頁面時,加載需要半分鐘。另外,這些功能根本不起作用。這段代碼似乎只適用於w3schools。他們提供了cdn(已經過時了,但仍然可以工作)。任何援助非常感謝。Angularjs不工作,頁面加載太慢

<!DOCTYPE html> 
 
<html> 
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
 
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-route.js"></script> 
 

 
<body ng-app="myApp"> 
 

 
<p><a href="#/">Main</a></p> 
 

 
<a href="#banana">Banana</a> 
 
<a href="#tomato">Tomato</a> 
 

 
<p>Click on the links to change the content.</p> 
 

 
<p>The HTML shown in the ng-view directive are written in the template property of the $routeProvider.when method.</p> 
 

 
<div ng-view></div> 
 

 
<script> 
 
var app = angular.module("myApp", ["ngRoute"]); 
 
app.config(function($routeProvider) { 
 
    $routeProvider 
 
    .when("/", { 
 
        template : "<h1>Main</h1><p>Click on the links to change this content</p>" 
 
    }) 
 
    .when("/banana", { 
 
        template : "<h1>Banana</h1><p>Bananas contain around 75% water.</p>" 
 
    }) 
 
    .when("/tomato", { 
 
        template : "<h1>Tomato</h1><p>Tomatoes contain around 95% water.</p>" 
 
    }); 
 
}); 
 
</script> 
 

 
</body> 
 
</html>

+0

只是一個側面說明我會建議你按照官方的角度網站。他們有一個很好的教程。 – abhishekkannojia

+0

好的。謝謝。 :) – thetekkenmaster

+0

'ngRoute'腳本包含丟失https: – Laazo

回答

1

加載時間可能是由於您的網絡連接,爲網頁需要下載從谷歌CDN角核心。本地加載可以解決它。

不過,我注意到你有這樣的代碼:

<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-route.js"></script> 

所以我不知道,你怎麼訪問你的HTML?你有本地主機服務器還是直接通過file:///協議?

如果你的答案是後者,那肯定不會工作,因爲angular-router的URL可能會自動添加前綴file:///協議,我猜也是這樣。

+0

非常感謝!我一直在通過文件協議訪問我的html。我很高興你爲我解釋了這個部分。我仍然是一個新手。 – thetekkenmaster

+0

@thetekkenmaster然後你需要學習在localhost上設置一個dev服務器。如果你想開發一些AJAX,它也是必需的。 – Leo

+0

我欣賞幫助球員。我想出瞭如何在本地主機上查看它。我下載了xampp。一切工作正常。 – thetekkenmaster

0

您使用的角度路徑文件無效。請使用有效的cdn網址

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.0/angular-route.min.js"></script>