Simple AngularJS routing system doesn't work (xampp server)

情到浓时终转凉″ 提交于 2019-12-13 06:49:45

问题


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.

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.

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...

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

Below the snippets of the files

<!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>'
    });
});

回答1:


DEMO

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>


来源:https://stackoverflow.com/questions/41908746/simple-angularjs-routing-system-doesnt-work-xampp-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!