【问题标题】:Angular2 animations [@triggerName] not working in *ngForAngular2 动画 [@triggerName] 在 *ngFor 中不起作用
【发布时间】:2017-02-07 17:59:23
【问题描述】:

基本上,我希望能够在满足条件时将动画应用到特定框。在我的应用程序中,当用户给出问题 1 的答案(在 Component1 中)-> 通过共享服务 -> 在 COMponent2 中,我会收到有关它的通知,如果答案正确,我将使用 id=1 缩放框(我用 id 只是为了指出盒子的特殊性的重要性)

*ngFor 添加的 [@triggerName] = "my_value_from_expression" 值无效,但该值存在;

如果我这样做:ng.probe($0)._debugInfo._view.changeDetectorRef._view 在盒子上,给我这个(_expr_3 是看起来的值):

...

_el_0:div#1.box

_expr_2:"1"

_expr_3:"animate1"

_expr_4:"Box1"

...

所以我有这个:

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

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

我尝试使用 ngfor 为 [@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>

前两个硬编码元素适用于 animate1animate2 变量,但其他两个使用 *ngFor 生成的元素根本不起作用。

我在这里做错了什么?

【问题讨论】:

  • 框:字符串[] = [];我认为你需要布尔数组
  • 查看构造函数 - 我的 box 数组如下所示:["animate1","animate2"]
  • 我用的是Augury,数组没问题
  • *ngFor 运行时,[@boxScale]="box" 之间存在问题
  • [@boxScale] 需要 true 或 false 而不是字符串。

标签: angular angular2-animation


【解决方案1】:

试试这样的如果下面的代码没有意义,请检查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]
  }

}

希望这会有所帮助..

【讨论】:

  • 请看我下面的回答——你的回答很好,但只适用于不可更改的值;如果我修改 animate1 值,第一个硬编码框可以工作,但第一次生成则不行(因为它的 boxScale 值是 true 而不是变量 `animate1)
【解决方案2】:

@MMK - 请查看update

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

【讨论】:

  • @MMK - 基本上我希望能够在满足条件时将我的动画应用到特定的框。在我的应用程序中,当用户为question1Component1)-> 通过共享服务-> 在COmponent2 中给出答案时,需要发生这种情况,我会收到有关它的通知,如果答案正确,我将扩展带有id=1的盒子(我用id只是为了指出盒子特异性的重要性)
  • 所以我尝试通过提供独特的 [@boxScale] 变量来实现这一点,例如:对于框 1=> [@boxScale] ="animate1" 对于框2 [@boxScale] ="animate2"
  • 然后在你的帮助下,我发现在我的数组中我推送了一个字符串而不是类对象,而对于 *ngFor,box 代表一个字符串而不是我最初认为的类对象 -另一方面,如果我将this.animate1 推入*ngFor 框中将是true or false(this.animate1 value) 而不是animate1 本身,就像我需要的那样
  • 我能以我开始的方式实现这一目标的唯一方法是硬编码 20 个具有特定 animate_n 值的 div 元素,但我不想这样做
  • 我也愿意接受其他建议
猜你喜欢
  • 1970-01-01
  • 2017-02-03
  • 2017-05-10
  • 2016-07-25
  • 2018-02-23
  • 2017-07-11
  • 2018-03-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多