gpt4 book ai didi

带参数的 AngularJs 路由

转载 作者:行者123 更新时间:2023-12-04 00:10:59 44 4
gpt4 key购买 nike

有人可以解释我如何使用参数路由到 URL 吗?

例如id 喜欢点击产品并通过Id打开产品的更多信息。

我的路由到目前为止...

        angular.module('shop', ["customFilters", "cart", "ngRoute"])
.config(function ($routeProvider){

$routeProvider.when("/complete", {
templateUrl: "../app/views/orderComplete.html"
});

$routeProvider.when("/placeorder", {
templateUrl: "../app/views/placeOrder.html"
});

$routeProvider.when("/checkout", {
templateUrl: "../app/views/checkoutSummary.html"
});

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

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

$routeProvider.otherwise({
templateUrl: "../app/views/productList.html"
});

});

所以通过点击...
<a class="btn btn-default" href="#/product/{{item.id}}">More Info</a>

我想被路由到 product/{{id}}.html ...

有人可以建议我在...
       $routeProvider.when("/product:id", {
templateUrl: "../app/views/product.html"
});

最佳答案

2件事,但你基本上就在那里。

首先,您在 URL 参数之前缺少斜线。发生在我们中最好的人身上。

routeProvider.when("/product/:id", {
templateUrl: "../app/views/product.html"
});

其次,当您有动态 URL 参数时,您应该使用 ng-href 代替 href。
<a ng-href="#/product/{{item.id}}">More Info</a>

关于带参数的 AngularJs 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39811845/

44 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com