简体   繁体   English

如何在ng-show中多次显示同一元素?

[英]How to show the same element more than once in ng-show?

I am playing around with a card game app, and I want a card image to show for each instance that is in the hand array. 我正在玩纸牌游戏应用程序,并且希望显示纸牌图像以显示手阵列中的每个实例。 To keep it simple, I am looking to show card1.png if the array looks like [1,2,3,4,5]. 为简单起见,如果数组看起来像[1,2,3,4,5],我希望显示card1.png。 However, I want the image to show twice if the array looks like [1,1,3,4,5]. 但是,如果数组看起来像[1,1,3,4,5],我希望图像显示两次。 So far, I can just get it to show once. 到目前为止,我只可以显示一次。

Index.html 的index.html

<div ng-repeat="hand in hands track by $index">
    Player {{$index}} hand: {{ hand }}
    <img src="/images/faces-one.png" ng-show="numberInArray(hand, 1)">
    <img src="/images/two.png" ng-show="numberInArray(hand, 2)">
    <img src="/images/three.png" ng-show="numberInArray(hand, 3)">
    <img src="/images/four.png" ng-show="numberInArray(hand, 4)">
    <img src="/images/five.png" ng-show="numberInArray(hand, 5)">
    <img src="/images/six.png" ng-show="numberInArray(hand, 6)">  
</div>

App.js App.js

$scope.numberInArray = function(hand, num) {
  var found;
  found = hand.includes(num) ? true : false;
  return found;
}

I'm not suggesting that this is the best solution, but to fix what you've currently got (or at least appear to have)... 我并不是说这是最好的解决方案,而是要修正您当前拥有的(或至少看起来有)的...

Use another (nested) ng-repeat on each image instead of ng-show . 在每个图像上使用另一个(嵌套的) ng-repeat而不是ng-show You should be able to attach it to a getNunbersInArray(n) function that returns a subset of the main array that can then be looped through the desired number of times. 您应该能够将其附加到getNunbersInArray(n)函数,该函数返回主数组的子集,然后可以将其循环所需的次数。

For example, if you have [1,1,3,4,5] in your main array, the function connected to the new ng-repeat s would return [1,1] (when passed 1) and [2] (when passed 2), etc. 例如,如果您的主数组中有[1,1,3,4,5] ,则连接到新ng-repeat的函数将返回[1,1] (传递1时)和[2] (通过2),等等。

You don't need to do anything with the values returned in these new arrays, the only thing that matters is that they contain the correct number of elements to ensure that the image is repeated the correct number of times. 您无需对这些新数组中返回的值做任何事情,唯一重要的是它们包含正确数量的元素,以确保图像重复正确的次数。

Following the comments I received, this is the solution I implemented: 根据收到的评论,这是我实现的解决方案:

Index.html 的index.html

<div ng-repeat="hand in hands track by $index">
     Player {{$index}} hand:
     <div ng-repeat="card in hand track by $index">
         <img ng-src="/images/{{card}}.png">
     </div>
</div>

The numberInArray function was removed as it was not required. 已删除numberInArray函数,因为它不是必需的。 Basically, This now iterates over the array of arrays, and displays the image found at the ng-src, which in this case would be 1.png, 2.png etc. This achieves my goal of showing the card image for each number in the array, regardless of its count. 基本上,这现在遍历数组的数组,并显示在ng-src处找到的图像,在这种情况下为1.png,2.png等。这实现了我的目标,即在数组,无论其数量如何。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM