Search code examples
angularanimationcycle

angular 2 animate in *ngFor only one element


I use animation in *ngFor. When i click and do animation, it animate all elements in cycle, but I want animate only one. How can I do that ? .........................................................................................................................................................

http://joxi.ru/82QYVWGSjBRGM2

   @Component({
    selector: 'app-popular-sensor',
    templateUrl: './popular-sensor.component.html',
    styleUrls: ['./popular-sensor.component.css'],
    animations: [
        trigger('focusPanel', [
            state('in', style({
                height: '20px'
            })),
            state('out', style({
                height: '*',
            })),
            transition('in => out', animate('400ms ease-in-out'))
        ])
    ]
})

export class PopularSensorComponent implements OnInit {

    goods;
    state = 'in';

    constructor(private goodsService: GoodsService) {
        goodsService.getGoods()
            .subscribe(goods => {
                this.goods = goods;
                console.log(this.goods);
            });
    }

    toggleChar() {
        this.state = this.state === 'in' ? 'out' : '';
    }

    ngOnInit() {

    }

}

html:

  <div *ngFor="let g of goods"
         class="col-sm-4 col-xs-12">
        <div class="sensor-block">

            <div class="char_block"
                 [@focusPanel]="state">
                <small *ngFor="let c of g.charact">{{c.name}}</small>
            </div>

            <div class="characteristic"
                 (click)="toggleChar()">Все характеристики смарт стола
            </div>
        </div>
    </div>

Solution

  • the "key" is not use a global variable, instead a variable belong to the object "g"

    <!--g.state-->
    <div class="char_block" [@focusPanel]="g.state">
      <small *ngFor="let c of g.charact">{{ c.name }}</small>
    </div>
    
    <!--change g.state-->
    <div class="characteristic" (click)="g.state=='in' ? 'out':'in'">
      Все характеристики смарт стола
    </div>