javascript - I am new to angularJS+express, and / in the routing path is converted to %2F, causing the path to become invalid. Can you tell me why?
漂亮男人
漂亮男人 2017-07-05 11:07:44
0
1
1190

The page opens as follows

Click the computer link and the url path will be displayed as follows

After clicking

The expected effect is as follows:

My code is as follows app.js, using "express": "^4.15.2"

var express = require('express');
var path = require('path');

var app = express();
//使用静态文件服务器中间件
app.use(express.static(path.join(__dirname,'app/public')));
app.listen(8080);

test.html


    <html ng-app="routingDemoApp">
<head>
    <meta charset="utf-8">
    <title>AngularJS 路由实例 - 菜鸟教程</title>
    <script src="/lib/jquery/dist/jquery.js"></script>
    <script src="/lib/bootstrap/dist/js/bootstrap.js"></script>
    <script src="/lib/angular/angular.js"></script>
    <script src="/lib/angular-route/angular-route.js"></script>
    <script>
        angular.module('routingDemoApp',['ngRoute'])
            .config(['$locationProvider','$routeProvider', function config($locationProvider, $routeProvider){

                $routeProvider
                    .when('/',{
                        template:'这是首页页面'
                    }).
                        when('/computers',{
                            template:'这是电脑分类页面'
                }).
                        when('/printers',{
                            template:'这是打印机页面'
                }).
                        otherwise('/');
            }]);
    </script>
</head>
<body>

<h2>AngularJS 路由应用</h2>
<ul>
    <li><a href="#/">首页</a></li>
    <li><a href="#/computers">电脑</a></li>
    <li><a href="#/printers">打印机</a></li>
    <li><a href="#/blabla">其他</a></li>
    <li><a href="#/printers">w3school</a></li>
</ul>

<p ng-view></p>



</body>
</html>

I have been stuck on this problem for a long time, please give me some direction, thank you

漂亮男人
漂亮男人

reply all(1)
Peter_Zhu

This is the default ! (exclamation mark) added to the hash route by angular1.6, causing an error. The modification method is as follows (add configuration, remove the default prefix exclamation mark):

    angular.module('routingDemoApp',['ngRoute'])
    .config(['$routeProvider', function($routeProvider){
        $routeProvider
        .when('/',{template:'这是首页页面'})
        .when('/computers',{template:'这是电脑分类页面'})
        .when('/printers',{template:'这是打印机页面'})
        .otherwise({redirectTo:'/'});
    }])
    //添加如下配置
    .config(['$locationProvider', function($locationProvider) {
        $locationProvider.hashPrefix("");
    }]);
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template