2017-02-06 44 views
1

基本上我希望能够当满足条件,我的动画应用到特定的盒子。在我的应用程序这方面的需求,当用户给出了问题1(中的Component1)的答案,发生 - >通过共享服务 - >在COMPONENT2我得到通知一下,如果答案是正确的,我将扩展与ID = 1盒(我用id只是指出框的特殊性的重要性)Angular2动画[@triggerName]不是* ngFor工作

[@triggerName] = "my_value_from_expression"值不工作添加*ngFor但存在的价值;

如果我这样做: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产生不工作在所有。

我在这里做错了什么?

+0

箱:字符串[] = [];你需要布尔数组我觉得在构造 – MMK

+0

看 - 我的箱子阵列看起来像这样:“animate1”,“animate2”] – sTx

+0

我用占卜和数组是确定 – sTx

回答

0

尝试类似这样:检查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] 
    } 

} 

希望这将有助于..
Output

+0

请看下面的答案 - 你的答案是好的,但只适用于不可改变的值;如果我修改animate1值第一个硬编码框的作品,但首先生成不(因为它的boxScale值是真的不变'animate1) – sTx

0

@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; 
} 
+0

@MMK - 基本上我想能够将我的动画应用到特定的盒子当条件满足。在我的应用程序中,当用户给出'question1'(在'Component1') - >通过共享服务 - >'在COmponent2'中得到答案时,我会收到通知,如果答案是正确的,我将按比例'id = 1'的盒子(我用id只是指出盒子特殊性的重要性) – sTx

+0

所以我试图通过给出独特的'[@boxScale]'变量来实现这个功能,如:box 1 => '[@boxScale] =“animate1”'box2'[@boxScale] =“animate2”' – sTx

+0

然后在您的帮助下,我发现在我的数组中,我推送一个字符串而不是一个类对象, ,box表示一个字符串,不像我最初认为的那样是类的对象 - 另一方面,如果我在'* ngFor'框中推'this.animate1'将会是true或false(this.animate1的值)而不是'animate1'本身就像我需要 – sTx