【问题标题】:angular 2 animate in *ngFor only one elementangular 2 animate in *ngFor only one element
【发布时间】:2018-03-02 06:40:37
【问题描述】:

我在 *ngFor 中使用动画。当我单击并做动画时,它会循环动画所有元素,但我只想动画一个。我怎样才能做到这一点 ? ..................................................... ..................................................... ..................................................... ...

   @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>

【问题讨论】:

  • 您可以尝试使用查询吗?更多参考这里yearofmoo.com/2017/06/…
  • 谢谢它的工作!但我认为我可以在不为每个元素创建数组的情况下做到这一点,无论如何这是一个很好的例子

标签: angular animation cycle


【解决方案1】:

“key”不是使用全局变量,而是属于对象“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>

【讨论】:

  • 我看不到它是如何工作的所有图片,但这个工作正常ng-run.com/edit/M5QOQGw0wruwXi0DiorT 谢谢!
  • @СтасРябцев 简要说明:你有一个对象数组,在一个已经存在的对象中,你可以简单地添加一个变量 myobject.newproperty=value (如果你写 {{goods |json }} 在你的.html 你可以看到它)。如果此对象没有属性“newpropery”,则 myobject.newproperty 将其添加到对象中,如果对象具有属性,则更改属性的值。我宁愿不调用函数,只使用三元运算符。 g.state=='in' 仅在 g.state 为 'in' 时为真(如果未定义,则不是条​​件)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-12-02
  • 2016-03-21
  • 2017-09-20
  • 2018-10-13
  • 2018-06-01
  • 2018-04-25
  • 2017-05-07
相关资源
最近更新 更多