仅在角度2中隐藏子元素相关的父元素

时间:2018-02-26 12:58:08

标签: angular hide parent-child

  

我是角度2的新手。如果我点击任何“按钮”,那只是下一个跨度   闭合。

<div class="parent">
<button>Click</button>
  <span>Element One</span>
</div>
<div class="parent">
<button>Click</button>
  <span>Element Two</span>
</div>
<div class="parent">
<button>Click</button>
  <span>Element Three</span>
</div>

1 个答案:

答案 0 :(得分:0)

试试这个: 的 DEMO

HTML:

<div class="parent1">
<button (click)="hide('parent1')">Click</button>
  <span #elOne>Element One</span>
</div>
<div class="parent2">
<button (click)="hide('parent2')">Click</button>
  <span #elTwo>Element Two</span>
</div>
<div class="parent3">
<button (click)="hide('parent3')">Click</button>
  <span #elThree>Element Three</span>
</div>

TS:

export class AppComponent  {

   hide(parentClass){
      let span = document.querySelector("." + parentClass + ' span');
      span.style.visibility = 'hidden'
  }
}