Hide/show individual items inside ngFor

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

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

  • 6条回答
    •  挽巷
      挽巷 (楼主)
      2020-11-28 09:11

      Add HTML ngFor by passing the values to the function.

      • {{ent.values}}

        {{ent.name}}

      declare variable.

       hideme = {};
       entities = [
          { id: 1, values: "Animal", name: "Tiger" },
          { id: 2, values: "Bird", name: "Sparrow" },
          { id: 3, values: "River", name: "Nile" }
        ];
      

      initiate it in the constructor and onInit.

        constructor() {
          this.hideme = {};
        }
      
        ngOnInit(): void {
          this.entities.forEach(e => {
            this.hideme[e.id] = false;
          });
        }
      
      

      set all values to false.

      then onclick .

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

      change flag of that perticular id to true and rest of them will be false

      Check stackblitz here!

    提交回复
    热议问题