【问题标题】:Why express4 with angularjs rewrite the url on browser?为什么 express4 用 angularjs 重写浏览器上的 url?
【发布时间】:2014-06-21 11:03:22
【问题描述】:

我做了一个演示,将 Express 4 与 AngularJS 集成。

效果很好,

http://localhost:3000/ 

在 index 的 ng-view 中显示欢迎页面,并且

http://localhost:3000/#/users 

在 ng-view 中显示用户页面。

但是当访问时

http://localhost:3000/#/users 

页面加载后,浏览器上的url变为

http://localhost:3000/users

这不是正确的道路。

我真的不知道为什么?

无论如何让它不改变?

有人知道吗?

app.js 中的路由器:

var web = require('./routes/web'); app.use('/', web);

....

/* GET home page. */
router.get('/', function(req, res) {
    //res.render('index', { title: 'Express' });
    res.sendfile('web/index.html');
});

AngularJS app.js:

var app = angular.module('app', ['ngRoute', 'shopControllers']);

app.config(['$routeProvider', '$locationProvider',
    function ($routeProvider, $locationProvider) {
        $routeProvider
            .when('/', {
                templateUrl: '/views/tpl/welcome.html',
                controller: 'WelcomeCtrl'})
            .when('/users', {
                templateUrl: '/views/tpl/users.html',
                controller: 'UserListCtrl'})
            .otherwise({redirectTo: '/'});
        $locationProvider.html5Mode(true);
    }]);

AngularJS 控制器:

var shopControllers = angular.module('shopControllers', []);

shopControllers.controller('WelcomeCtrl', ['$scope', '$routeParams',
    function($scope, $routeParams) {
        $scope.username = 'Conan_Z';
    }]);

shopControllers.controller('UserListCtrl', ['$scope', '$http',
    function($scope, $http) {
        $http.get('service/user/get.json?shopVipId=2').success(function(data) {
            $scope.users = data;
        });

        $scope.orderProp = 'age';
    }]);

AngularJS 索引.html:

    <div class="row">
        <div class=".col-lg-12">
            <div ng-view></div>
        </div>
    </div>

【问题讨论】:

    标签: node.js angularjs express


    【解决方案1】:

    这与快递无关。

    这是由于 Angular js 位置提供程序正在使用历史 API。

    在你的路线中删除

     $locationProvider.html5Mode(true);
    

    这会解决你的问题。

    【讨论】:

      【解决方案2】:

      删除下面的这一行, 这基本上删除了支持 html5 的网络浏览器的 #,这不是一件坏事,但为了兼容性问题,最好现在有 #。

      $locationProvider.html5Mode(true);
      

      【讨论】:

      • 嗨 z.a,我是 Stackoverflow 的新手,这是我的第一个问题。如何给你信用?点击向上按钮?
      • 另外,您可以在向上按钮的正下方打勾,作为正确答案。谢谢。
      猜你喜欢
      • 2021-11-12
      • 1970-01-01
      • 1970-01-01
      • 2017-06-26
      • 1970-01-01
      • 2011-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多