简单的AngularJS路由系统不起作用(XAMPP服务器) [英] Simple AngularJS routing system doesn't work (xampp server)
问题描述
我第一次尝试Angular,而我要实现的第一个问题是单页应用程序功能.但是这里的第一个问题是:我配置了一个非常简单的工作,它分为两个文件:index.html和project.js.
I'm trying Angular for the first time, and my first issue that I want to implement is single page application feature. But here the first problem: I have configured a very simple work, it is divided into two files: index.html and project.js.
我正在Windows 10机器上工作,并且正在使用xampp 3.2.2,该项目放置在/htdocs/webapp文件夹中.
I'm working on a Windows 10 machine and I'm using xampp 3.2.2, this project is putted inside the folder /htdocs/webapp.
访问"localhost/webapp/"时,它会正确加载其他"条件,但是当我尝试加载"localhost/webapp/#/tomato"或"localhost/webapp/#tomato"时,URL变为" http://localhost/webapp/#!#tomato ",页面仍显示内容否则的情况...
When access to "localhost/webapp/" it properly load the "otherwise" condition, but when I try to load for example "localhost/webapp/#/tomato" or "localhost/webapp/#tomato" the url becomes "http://localhost/webapp/#!#tomato" and the page still shows the content of the otherwise condition...
我真的不知道是什么原因,而且控制台也没有显示任何错误,似乎在$ routeProvider中总是出现其他情况.
I really don't know what is the cause, and also the console doesn't show any errors, it seems that in $routeProvider always get the otherwise condition.
请大家给我一些解决方案,我很紧张;-)谢谢
Please guys, give some solutions I'm very nervous ;-) Thank you
文件摘要下方
<!doctype html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular-route.min.js"></script>
<script src="project.js"></script>
</head>
<body ng-app="myApp">
<h2>JavaScript Projects</h2>
<div ng-view></div>
</body>
</html>
var app = angular.module("myApp", ["ngRoute"]);
app.config(function($routeProvider) {
$routeProvider
.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>"
})
.otherwise({
template : '<h1>None</h1><p>Nothing has been selected</p><a href="#tomato">prova</a>'
});
});
推荐答案
演示
var app = angular.module("contactMgr", ['ngRoute']);
app.config(['$routeProvider', function($routeProvider) {
$routeProvider
.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>"
})
.otherwise({
template : '<h1>None</h1><p>Nothing has been selected</p><a href="#tomato">prova</a>'
});
}])
<!DOCTYPE html>
<html ng-app="contactMgr">
<head>
<meta charset="UTF-8" />
<meta name="description" content="" />
<meta name="keywords" content="" />
<meta name="robots" content="" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
<link data-require="bootstrap-css@*" data-semver="4.0.0-alpha.4" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.4/css/bootstrap.min.css" />
<script src="//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>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script src="app.js"></script>
<link rel="stylesheet" href="style.css" />
<title>Title of the document -1</title>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-sm-12">
<h1>Welcome to Route</h1>
<div class="nav">
<a href="#tomato" class="button">Tomato</a>
<a href="#banana" class="button">Banana</a>
</div>
</div>
</div>
</div>
<div class="container">
<div ng-view=""></div>
</div>
</body>
</html>
这篇关于简单的AngularJS路由系统不起作用(XAMPP服务器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!