简体   繁体   English

Angular2动画[@triggerName]在* ngFor中不起作用

[英]Angular2 animations [@triggerName] not working in *ngFor

Basically I want to be able to apply my animation to a specific box when a condition is fulfilled. 基本上我希望能够在条件满足时将动画应用到特定的框中。 In my app this need to happen when the user give an answer for question1 (in Component1) -> through a shared service -> in COmponent2 I get notified about it, and if the answer is correct I will scale the box with id=1(I used id just to point the importance of the specificity of the box) 在我的应用程序中,当用户给出question1的答案(在Component1中) - >通过共享服务 - >在COmponent2中时,我需要收到通知,如果答案是正确的,我会缩放id = 1的框(我用id来指出盒子特异性的重要性)

The [@triggerName] = "my_value_from_expression" value not working added by *ngFor but the value exists; [@triggerName] = "my_value_from_expression"值不工作由*ngFor添加,但值存在;

If I do this: ng.probe($0)._debugInfo._view.changeDetectorRef._view on the box, gives me this (_expr_3 is the looked value): 如果我这样做: ng.probe($0)._debugInfo._view.changeDetectorRef._view在框中,给我这个(_expr_3是看起来的值):

... ...

_el_0:div#1.box _el_0:DIV#1.box

_expr_2:"1" _expr_2: “1”

_expr_3:"animate1" _expr_3: “animate1”

_expr_4:"Box1" _expr_4: “BOX1”

... ...

So I have this: 所以我有这个:

frame.component.ts frame.component.ts

@Component({
selector: 'app-frame',
templateUrl: './frame.component.html',
styleUrls: ['./frame.component.css'],
animations:[ 
  trigger('boxScale',[
    state('true', style({
        transform: 'scale(1.2)',
      })),
    state('false', style({
    transform: 'scale(1)',
  })), 
  transition('false => true', animate('100ms ease-in')),
    transition('true => false', animate('100ms ease-out')),
  ]),]
})
export class FrameComponent implements OnInit {
  answ:string[];
  boxes: string[] = [];
  animate1:boolean = true;
  animate2:boolean = false;

constructor(private _commService: CommunicateService) { 
  this._commService.answerSource.subscribe((result:string[]) => {
    this.answ = result;
    this.animate(result)
})

  for(let i = 1; i <= 2; i++ ){
    this.boxes.push("animate"+i);
  }
}
animate(result){
 /***** result: ['q_id',corect_answ','user_answ'] *****/
  if(result[1] != undefined && result[1] === result[2]){
      this.animate1 = !this.animate1;
      this.animate2 = !this.animate2;
    }
  }
}

frame.component.html frame.component.html

<div class="boxes">
   <div class="box" [@boxScale]="animate1">Box_1</div>
   <div class="box" [@boxScale]="animate2">Box_2</div>
   <div *ngFor="let box of boxes; let i = index; " class="box" id="{{i+1}}" [@boxScale]="box">Box{{i+1}}</div>
</div>

What I try to do is to add with ngfor the correct value for [@boxScale] so the result should be: 我尝试做的是添加ng为[@boxScale]的正确值,所以结果应该是:

<div class="boxes">
  <div class="box" [@boxScale]="animate1">Box_1</div>
  <div class="box" [@boxScale]="animate2">Box_2</div>
  <div class="box" id="1" [@boxScale]="animate1">Box1</div>
  <div class="box" id="2" [@boxScale]="animate2">Box2</div>
</div>

First two hardcoded elements works just fine with those animate1 and animate2 variables but the other two, generated with *ngFor are not working at all. 前两个硬编码元素与那些animate1animate2变量一起正常工作,但另外两个用*ngFor生成的*ngFor根本不起作用。

What am I doing wrong here? 我在这做错了什么?

Try something like this Check the Plunker if code below doesn't make sense. 尝试这样事情如果下面的代码没有意义,请检查Plunker

import { 
    Component, OnChanges, Input, 
    trigger, state, animate, transition, style 
} from '@angular/core';

@Component({
  selector : 'ease-inout',
  animations: [
    trigger('boxScale',[
    state('true', style({
        transform: 'scale(1.2)',
      })),
    state('false', style({
    transform: 'scale(1)',
  })), 
  transition('false => true', animate('100ms ease-in')),
    transition('true => false', animate('100ms ease-out')),
  ])
  ],
  template: `

    <div class="boxes">
      <div class="box" [@boxScale]="animate1">Box_1</div>
      <div class="box" [@boxScale]="animate2">Box_2</div>
    <div *ngFor="let box of boxes; let i = index; " class="box" id="{{i+1}}" [@boxScale]="box">Box{{i+1}}</div>
</div>
  `
})
export class EaseInOutComponent implements OnChanges {
  boxes: boolean[] = [];
  animate1:boolean = true;
  animate2:boolean = false;

  constructor()
  {
    // just for example
    this.boxes.push(this.animate1);
    this.boxes.push(this.animate2);
    console.log(this.boxes) //[true, false]
  }

}

Hope this will help.. 希望这会有所帮助..
产量

@MMK - please check the update @MMK - 请检查更新

 <div>
    <button (click)="box1()">Toggle Box1</button>
    <button (click)="box2()">Toggle Box2</button>
</div>    
box1(){
  this.animate1 = !this.animate1;
}
box2(){
  this.animate2 = !this.animate2;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM