angular-route1.6.1无法正常工作

时间:2017-01-27 08:31:59

标签: javascript angularjs angularjs-1.6

谢谢你的期待! 这是我的问题。

[app.html]
...
<custom />
...
<script>
...
app.config(["$routeProvider", function ($routeProvider) {
    $routeProvider.when("/checkout", {
        templateUrl: "views/checkoutSummary.html",
    });

    $routeProvider.when("/products", {
        templateUrl: "views/productList.html"
    });

    $routeProvider.otherwise({
        templateUrl: "/views/productList.html",
    })
}])
...
</script>

“custom”的指令在这里:

<div class="navbar-right">
    <div class="navbar-text">
        <b>购物车:</b>
        {{itemCount()}} 个商品,
        {{total() | currency}}
    </div>
    <a href="#/checkout" class="btn btn-default navbar-btn">结算</a>
</div>

当我点击元素“a”时,浏览器中的href为:

http://localhost:3000/app.html#!#%2Fcheckout

$location.hash()/checkout

实际上,正确的href应该是:

http://localhost:3000/app.html#!/checkout

http://localhost:3000/app.html#!%2Fcheckout

为什么程序在“!”后添加“#”?

Tx和新年快乐!

1 个答案:

答案 0 :(得分:2)

的Tx!我解决了! 原因是版本1.6.1中的路由规则已更改。 编写网址的正确方法是:

href="#!/checkout"

另一种解决这个问题的方法:

app.config(['$locationProvider', function($locationProvider) {
  $locationProvider.hashPrefix('');
}]);

如下: https://github.com/angular/angular.js/commit/aa077e81129c740041438688dff2e8d20c3d7b52