Angularjs无法正常工作,页面加载速度太慢

时间:2016-11-30 06:09:03

标签: javascript angularjs

我正在浏览w3schools的教程,我复制了他们的一个片段。当我尝试在浏览器中查看页面时,需要半分钟才能加载。而且,这些功能根本不起作用。此代码似乎仅适用于w3schools。他们提供了cdn(已过时但应该仍然有效)。非常感谢任何帮助。



<!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>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

加载时间可能是由于您的网络连接,因为页面需要从谷歌CDN下载角度核心。在本地加载它可以解决它。

但是,我注意到你有这样的代码:

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

所以我想知道,您是如何访问HTML的?您是否有本地主机服务器或直接通过file:///协议?

如果您的答案是后者,那么它肯定不会起作用,因为angular-router的URL也可以通过file:///协议自动加上前缀,我猜是404。

答案 1 :(得分:0)

您使用的角度路径文件无效。请使用有效的cdn网址

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