Hide/show individual items inside ngFor

后端 未结 6 641
独厮守ぢ
独厮守ぢ 2020-11-28 08:31

I need to show / hide part of component. Here is Angular2 example.

  • 6条回答
    •  暖寄归人
      2020-11-28 09:32

      You're hideme variable is global. Perhaps you could attach it to the current item:

    • Click
      Hide
    • Otherwise you need to use a dedicated object object from your component. Here is a sample:

    • Click
      Hide
    • Don't forget to initialize the hideme object this way in your component:

      hideme: = {};
      

      Edit

      If you want to make this work like tabs, you need a bit more work ;-)

    • Click
      Hide
    • And to display the clicked element and hide others:

      onClick(item) {
        Object.keys(this.hideme).forEach(h => {
          this.hideme[h] = false;
        });
        this.hideme[item.id] = true;
      }
      

    提交回复
    热议问题