本文介绍了推动并阅读里面嵌套,从阵列和NG-赘述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新2:,它的工作原理


更新:


我试图将一个阵列内的秩序,并显示其项目(以及其中有多少),但我不知道我做错了什么。我只是不能访问比萨饼 orderList 与NG-重复的名单。

我什么code应该做的是:


  • 读取存储阵列内的菜单

  • 单击某个项目,将其存储另一个数组里面

  • 如果它是被存储在第一时间,分配 N = 1 到元素

  • 其他 N + = 1

  • 显示您的顺序读取 pizza.name pizza.n orderList

  • 删除存储在 orderList 项目

code.javascript

  $ scope.orderList = [];$ scope.add =功能(比萨){
  变种N;
  $ scope.placeholder ='为Aggiungi雅卓?';
  如果($ scope.orderList.indexOf(比萨)=== -1){
    $ scope.orderList.pizza = [];
    N = 1;
    返回$ scope.orderList.pizza.push(pizza.name,N);
  }其他{
    返回$ scope.orderList.pizza.n + = 1;
  }
};$ scope.remove =功能(比萨){
  VAR指标;
  指数= $ scope.orderList.indexOf(比萨);
  $ scope.orderList.pizza.splice(指数,1);
  如果($ scope.orderList.length === 0){
    返回$ scope.example();
  }
};

form.html

 < UL NG隐藏=清单级=清单>
  <李NG重复=比萨饼pizze |过滤器:搜索|排序依据:'名'>
    <按钮NG点击=添加(比萨)级=添加> {{pizza.name}}< BR> {{pizza.ingredients}}< /按钮>
  < /李>
< / UL>
< UL NG隐藏=订单级=清单>
  <李NG重复=比萨orderList>
    <按钮NG点击=删除()级=添加> Rimuovi {{pizza.name +'X'+ pizza.n}}< /按钮>
  < /李>
< / UL>


解决方案

应该可以简化到

  $ scope.add =功能(比萨){
  pizza.n =(pizza.n || 0)+1; //计数更新无论是否数组或不
  如果($ scope.orderList.indexOf(比萨)=== -1){
    $ scope.orderList.push(比萨);
  } //如果已经在阵列无关
};$ scope.remove =功能(比萨){
  pizza.n = NULL; //以防万一用户将它放回秩序
  VAR指数= $ scope.orderList.indexOf(比萨);
  如果(指数=== -1)回报;
  $ scope.orderList.splice(指数,1);
  如果(!$ scope.orderList.length){
     $ scope.example();
  }
};

UPDATE 2: forked plnkr, it works.


UPDATE: plnkr


I'm trying to place an order inside an array, and display its items (and how many of them), but I don't know what I'm doing wrong. I just can't access the list of pizzas inside orderList with ng-repeat.

What my code should do is:

  • read the menu stored inside an array
  • click an item to store it inside another array
  • if it's the first time that is being stored, assign n=1 to that element
  • else n+=1
  • display your order reading pizza.name and pizza.n from orderList
  • remove items stored in orderList

code.javascript

$scope.orderList = [];

$scope.add = function(pizza) {
  var n;
  $scope.placeholder = 'Aggiungi altro?';
  if ($scope.orderList.indexOf(pizza) === -1) {
    $scope.orderList.pizza = [];
    n = 1;
    return $scope.orderList.pizza.push(pizza.name, n);
  } else {
    return $scope.orderList.pizza.n += 1;
  }
};

$scope.remove = function(pizza) {
  var index;
  index = $scope.orderList.indexOf(pizza);
  $scope.orderList.pizza.splice(index, 1);
  if ($scope.orderList.length === 0) {
    return $scope.example();
  }
};

form.html

<ul ng-hide="list" class="list">
  <li ng-repeat="pizza in pizze | filter:search | orderBy: 'name'">
    <button ng-click="add(pizza)" class="add">{{pizza.name}}<br>{{pizza.ingredients}}</button>
  </li>
</ul>
<ul ng-hide="order" class="list">
  <li ng-repeat="pizza in orderList">
    <button ng-click="remove()" class="add">Rimuovi {{pizza.name + ' x' + pizza.n}}</button>
  </li>
</ul>
解决方案

Should be able to simplify this down to

$scope.add = function(pizza) {
  pizza.n = (pizza.n || 0) +1; // count updates regardless if in array or not
  if ($scope.orderList.indexOf(pizza) === -1) {    
    $scope.orderList.push(pizza);    
  }// if already in array nothing to do
};

$scope.remove = function(pizza) {
  pizza.n = null;// just in case user puts it back into order
  var index = $scope.orderList.indexOf(pizza);
  if(index === -1) return;
  $scope.orderList.splice(index, 1);
  if (!$scope.orderList.length ) {
     $scope.example();
  }
};

这篇关于推动并阅读里面嵌套,从阵列和NG-赘述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 05:54