Angularjs不工作,页面加载太慢
我正在浏览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>
加载时间可能是由于您的网络连接,为网页需要下载从谷歌CDN角核心。本地加载可以解决它。
不过,我注意到你有这样的代码:
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-route.js"></script>
所以我不知道,你怎么访问你的HTML?你有本地主机服务器还是直接通过file:///
协议?
如果你的答案是后者,那肯定不会工作,因为angular-router的URL可能会自动添加前缀file:///
协议,我猜也是这样。
非常感谢!我一直在通过文件协议访问我的html。我很高兴你为我解释了这个部分。我仍然是一个新手。 – thetekkenmaster
@thetekkenmaster然后你需要学习在localhost上设置一个dev服务器。如果你想开发一些AJAX,它也是必需的。 – Leo
我欣赏帮助球员。我想出了如何在本地主机上查看它。我下载了xampp。一切工作正常。 – thetekkenmaster
您使用的角度路径文件无效。请使用有效的cdn网址
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.0/angular-route.min.js"></script>
只是一个侧面说明我会建议你按照官方的角度网站。他们有一个很好的教程。 – abhishekkannojia
好的。谢谢。 :) – thetekkenmaster
'ngRoute'脚本包含丢失https: – Laazo