【问题标题】:How to make menu work with ngRoute on AngularJS?如何使菜单在 AngularJS 上与 ngRoute 一起使用?
【发布时间】:2017-09-10 13:59:23
【问题描述】:

如何使菜单像在线商店中的普通菜单一样工作?我有一个菜单,当我单击元素时,它应该将我重定向到 myshop.com/food,其中食物取决于在菜单中单击的内容。它应该与 ngRoute 一起使用,但是如何让我的产品出现在那里?也许它可以以某种方式对我的对象进行 http 调用?

ngRoute:

mainApp.config(function ($routeProvider) {
$routeProvider.when("/pizza", {
    templateUrl: "food.html"
}).otherwise({
    templateUrl: "food.html"
});

菜单代码:

<li><a href="#!pizza">Pizza</a></li> <li><a href="#!burgers">Burgers</a></li> <ng-view></ng-view>

food.html

   <div class="food-show-block">
            <div class="block-container">
                <div class="block item-block food-block" id="foodBlock" ng-repeat="product in food.slice(((currentPage-1)*itemsPerPage), ((currentPage)*itemsPerPage)) | filter:search" ng-mouseenter="showBasket=true" ng-mouseleave="showBasket=false">
                    <div class="basket" ng-show="showBasket"> <i class=" fa fa-shopping-basket fa-4x" aria-hidden="true"></i> </div>
                    <a href="#"> <img src="{{product.imageLink}}" alt="">
                        <div class="details">
                            <p class="price">{{product.price}}грн</p>
                            <p class="name"> {{product.name}}</p>

                        </div>
                    </a>
                </div>

            </div>
        </div>

【问题讨论】:

  • 这段代码有一些问题;你开始说你想要/food,但你显示路线/pizza,否则,两条路线完全相同。看来您需要对您正在尝试做的事情做更多的研究。

标签: javascript html angularjs ngroute


【解决方案1】:

通过使用控制器

使用$routeProvider,您还可以为每个view 定义一个控制器,如下所示:

mainApp.config(function ($routeProvider) {
$routeProvider.when("/pizza", {
    templateUrl: "food.html",
    controller : "pizzaCtrl"
}).otherwise({
    templateUrl: "food.html",
    controller : "foodCtrl"
});

在你的controller编写逻辑就像调用http服务

mainApp.controller('foodCtrl', function($scope, $http) {
    $http.get("httpGetUrl")
      .then(function(response) {
       $scope.yourObject = response.data;//response from get
       });    
});

【讨论】:

  • 点击菜单链接会调用http吗?
猜你喜欢
  • 1970-01-01
  • 2023-03-07
  • 2014-08-30
  • 2014-11-30
  • 1970-01-01
  • 2016-05-11
  • 1970-01-01
  • 2020-01-20
  • 2016-06-22
相关资源
最近更新 更多