单击时显示ngFor Array的子​​内容

时间:2019-09-13 00:42:12

标签: json angular ngfor

在我的项目中,屏幕上显示的每个卡值均使用ngFor从JSON循环过来。理想的目标是当用户单击卡片时,它仅显示来自JSON的卡片信息,而仅显示带有* ngIf的div内容。我创建了一个动画,以淡入想要显示内容的蒙版中。当前,如果您单击该卡,它将仅显示缩略图阵列。我没有任何错误或其他任何事情。我不知疲倦地寻找有关如何完成单击显示单个键的答案。我需要一次显示一张卡片的卡片图像,名称和描述。我觉得自己遇到了障碍,没有在谷歌搜索正确的描述。如果需要进一步说明,请告诉我。谢谢您提供的任何指导。

    [
  {
    "id": 1,
    "content": [
      {
      "sin": "Vanity",
      "card": "/assets/img/vanity.jpg",
      "icon": "/assets/img/vanityIcon.jpg",
      "info": "In almost every list pride (or hubris or vanity) is considered the original and most serious of the seven deadly sins, and indeed the ultimate source from which the others arise. It is identified as a desire to be more important or attractive than others, failing to acknowledge the good work of others, and excessive love of self (especially holding self out of proper position toward God). Dante's definition was 'love of self perverted to hatred and contempt for one's neighbor.' In Jacob Bidermann's medieval miracle play, Cenodoxus, pride is the deadliest of all the sins and leads directly to the damnation of the titulary famed Parisian doctor. In perhaps the best-known example, the story of Lucifer, pride (his desire to compete with God) was what caused his fall from Heaven, and his resultant transformation into Satan. Vanity and narcissism are prime examples of this sin. In Dante's Divine Comedy, the penitents were forced to walk with stone slabs bearing down on their backs in order to induce feelings of humility."
      }
    ]
  },
  {
    "id": 2,
    "content": [
      {
      "sin": "Envy",
      "card": "/assets/img/envy.jpg",
      "icon": "/assets/img/envyIcon.jpg",
      "info": "Like greed, envy may be characterized by an insatiable desire; they differ, however, for two main reasons. First, greed is largely associated with material goods, whereas envy may apply more generally. Second, those who commit the sin of envy resent that another person has something they perceive themselves as lacking, and wish the other person to be deprived of it. Dante defined this as 'love of one's own good perverted to a desire to deprive other men of theirs.' In Dante's Purgatory, the punishment for the envious is to have their eyes sewn shut with wire because they have gained sinful pleasure from seeing others brought low. Aquinas described envy as 'sorrow for another's good'."
      }
    ]
  },
  {
    "id": 3,
    "content": [
      {
      "sin": "Sloth",
      "card": "/assets/img/sloth.jpg",
      "icon": "/assets/img/slothIcon.jpg",
      "info": "More than other sins, the definition of sloth has changed considerably since its original inclusion among the seven deadly sins. In fact it was first called the sin of sadness or despair. It had been in the early years of Christianity characterized by what modern writers would now describe as melancholy: apathy, depression, and joylessness — the last being viewed as being a refusal to enjoy the goodness of God and the world God created. Originally, its place was fulfilled by two other aspects, acedia and sadness. The former described a spiritual apathy that affected the faithful by discouraging them from their religious work. Sadness (tristitia in Latin) described a feeling of dissatisfaction or discontent, which caused unhappiness with one's current situation. When Thomas Aquinas selected acedia for his list, he described it as an 'uneasiness of the mind', being a progenitor for lesser sins such as restlessness and instability. Dante refined this definition further, describing sloth as being the 'failure to love God with all one's heart, all one's mind and all one's soul.' He also described it as the middle sin, and as such was the only sin characterised by an absence or insufficiency of love. In his 'Purgatorio', the slothful penitents were made to run continuously at top speed."
      }
    ]
  },
  {
    "id": 4,
    "content": [
      {
      "sin": "Wrath",
      "card": "/assets/img/wrath.jpg",
      "icon": "/assets/img/wrathIcon.jpg",
      "info": "Wrath (or anger or 'Rage') may be described as inordinate and uncontrolled feelings of hatred and anger. These feelings can manifest as vehement denial of the truth, both to others and in the form of self-denial, impatience with the procedure of law, and the desire to seek revenge outside of the workings of the justice system (such as engaging in vigilantism) and generally wishing to do evil or harm to others. The transgressions borne of vengeance are among the most serious, including murder, assault, and in extreme cases, genocide. Wrath is the only sin not necessarily associated with selfishness or self-interest (although one can of course be wrathful for selfish reasons, such as jealousy, closely related to the sin of envy). Dante described vengeance as 'love of justice perverted to revenge and spite'. In its original form, the sin of wrath also encompassed anger pointed internally rather than externally. Thus suicide was deemed as the ultimate, albeit tragic, expression of wrath directed inwardly, a final rejection of God's gifts."
      }
    ]
  },
  {
    "id": 5,
    "content": [
      {
      "sin": "Lust",
      "card": "/assets/img/lust.jpg",
      "icon": "/assets/img/lustIcon.jpg",
      "info": "Lust (or lechery) is usually thought of as excessive thoughts or desires of a sexual nature. Giving in to lusts can lead to sexual or sociological compulsions and/or transgressions including (but not limited to) sexual addiction, fornication, adultery, bestiality, rape, perversion, and incest. Dante's criterion was 'excessive love of others', which therefore rendered love and devotion to God as secondary. In 'Purgatorio', the penitent walks within flames to purge himself of lustful/sexual thoughts and feelings."
      }
    ]
  },
  {
    "id": 6,
    "content": [
      {
      "sin": "Gluttony",
      "card": "/assets/img/gluttony.jpg",
      "icon": "/assets/img/gluttonyIcon.jpg",
      "info": "Derived from the Latin gluttire, meaning to gulp down or swallow, gluttony is the over-indulgence and over-consumption of anything to the point of waste. In the Christian religions, it is considered a sin because of the excessive desire for food, or its withholding from the needy. Depending on the culture, it can be seen as either a vice or a sign of status. Where food is relatively scarce, being able to eat well might be something to take pride in (although this can also result in a moral backlash when confronted with the reality of those less fortunate). Where food is routinely plentiful, it may be considered a sign of self-control to resist the temptation to over-indulge."
      }
    ]
  },
  {
    "id": 7,
    "content": [
      {
      "sin": "Greed",
      "card": "/assets/img/greed.jpg",
      "icon": "/assets/img/greedIcon.jpg",
      "info": "Greed (or avarice, covetousness) is, like lust and gluttony, a sin of excess. However, greed (as seen by the church) is applied to the acquisition of wealth in particular. St. Thomas Aquinas wrote that greed was 'a sin against God, just as all mortal sins, in as much as man condemns things eternal for the sake of temporal things.'' In Dante's Purgatory, the penitents were bound and laid face down on the ground for having concentrated too much on earthly thoughts. 'Avarice' is more of a blanket term that can describe many other examples of greedy behavior. These include disloyalty, deliberate betrayal, or treason,[citations needed] especially for personal gain, for example through bribery . Scavenging[citation needed] and hoarding of materials or objects, theft and robbery, especially by means of violence, trickery, or manipulation of authority are all actions that may be inspired by greed. Such misdeeds can include simony, where one profits from soliciting goods within the actual confines of a church."
      }
    ]
  }
]

(已更新为包含完整的JSON)

    <!-- CARD CONTENT -->
    <div class="table" *ngFor="let sins of cardwork; index as i">
    <!-- CARD CONTENT -->
        <div class="cardsLayedOut">
          <a href="#" (click)="toggleCard(i)"> // User Clicks here
            <img class="card" [ngStyle]="rotateCards()" *ngFor="let sublist of sins.content" [src]="sublist.card" />
          </a>
        </div>

        <div class="" *ngIf="cards" #cards [@clickevent]>
          <div class="mask columnCentered">
            <div class="contentLayout" *ngFor="let sublist of sins[i]?.content">
              <img class="largeCard" [src]="sublist[i]?.card" />
              <div class="sinInfo">
                <h3>{{sublist[i]?.sin}}</h3>
                <p>{{sublist[i]?.info}}</p>
              </div>
            </div>
            <app-thumnail></app-thumnail>
          </div>
        </div>
      </div>

我用于隐藏/显示卡信息的切换开关是


    toggleCard() {
        this.cards = true;
      }

enter image description here,这是添加纸牌持卡人= []时的当前状态;在我的toggleCard(i)中,this.cards [i] =!this.cards [i]; 第二张图像是每张卡的期望状态。 enter image description here

1 个答案:

答案 0 :(得分:1)

您的切换功能不正确。

您正在从模板i发送索引(click)="toggleCard(i)"作为参数,但是没有将其捕获到组件中。

您应该捕获该索引i,因为它唯一地标识了所点击的卡片。如果没有,您怎么知道单击了哪张卡?

toggleCard(var i) {
  // Handle click event.
}

此外,您仅使用一个cards变量来切换所有卡的状态。因此,如果单击一张卡,则所有卡都将显示或隐藏,而不仅仅是单击的卡。

您应该有一个数组cards[],该数组采用boolean的值并跟踪每张卡的切换状态。

toggleCard()函数中,每当单击卡片时,都根据接收到的索引i在此数组中切换适当的值。

var cards = [];  // Initialize with boolean values. 
                 // Array length would be equal to the number of your cards.   

toggleCard(var i) {
  this.cards[i] = !this.cards[i];
}

编辑:

关于实施建议的更改后的问题:

在您的enter image description here中:

  • 您正在将selectedCard初始化为false,但是此变量采用number值。这应该不是问题,因为JavaScript允许动态类型。
  • 此语句sin中有一个错字sins而不是<p>{{ sin.content.sin }}</p>。应该是<p>{{ sins.content.sin }}</p>

修复这些问题,您应该会看到所有卡片。