Angular如何做多个打开和关闭

时间:2019-01-24 08:35:51

标签: javascript angular

如果要单击以查找所有相同的ID,我想这样做。

并且所有相同的id的isOpen一起变为true。

该怎么做?

html

<div  *ngFor="let card of cards">
    <div class="open-close-container" (click)="test(card)">
      <div *ngIf="card.isOpen">test</div>
      <div *ngIf="!card.isOpen">shidsfsdfsdf<br>t</div>
    </div>
</div>

ts

 cards = [{name:'div1',id: 1,isOpen: false},{name:'div2',id: 1 ,isOpen: false},{name:'div1',id: 2,isOpen: false},{name:'div2',id: 3 ,isOpen: false}];

  test(q){
    console.log(q)
        q.isOpen = !q.isOpen
  }

这里是stackblitz

2 个答案:

答案 0 :(得分:0)

在您的InputConnectionWrapper函数中,将test(q)更改为:

q.isOpen = !q.isOpen

从本质上讲,当您调用test(q){ this.cards.filter((card) => q.id == card.id).map((q) =>{ q.isOpen = !q.isOpen}); } 时,卡片阵列会用匹配的test()进行过滤,只有那些对象被更改为id

这是经过修改的stackblitz

答案 1 :(得分:0)

将test()更改为:

    test(c){
    for (let card in this.cards)
        if(cards[card].id == c.id)
            cards[card].isOpen = !cards[card].isOpen
}