2017-02-06 4 views
1

基本的には、条件が満たされたときに特定のボックスにアニメーションを適用できるようにします。私のアプリでは、これは、ユーザが質問1(Component1の場合) - >共有サービス - > COmponent2で回答を得たときに発生する必要があります。これについて通知を受け、答えが正しい場合はid = 1 (私はちょうどボックスの特異性の重要性を指摘するためにIDを使用しました)Angular2アニメーション[@triggerName]が* ngForで動作しない

*ngForで値が機能していませんが値が存在します。

私はこれを行う場合は、次のボックスにng.probe($0)._debugInfo._view.changeDetectorRef._viewを、私は、この(_expr_3が見えた値である)が得られる:

を...

_el_0:DIV#1 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> 

何を私

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

第2のハードコード要素は、それらのanimate1animate2変数とうまく動作しますが、*ngForで生成された他の2つは、動作していないされていますので、結果がどうあるべきで追加することですngfor [@boxScale]の正しい値やろうまったく。

私はここで間違っていますか?

+0

ボックスをチェックしてください:文字列[] = [];あなたはブール値の配列が必要です私は – MMK

+0

のコンストラクタを見て - 私の箱の配列はこのように見えると思います:["animate1"、 "animate2"] – sTx

+0

私はAuguryを使用していて、配列はOKです – sTx

答えて

0

Plunker次のコードが意味をなさない場合は、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]'変数を与えることによってこれを達成しようとしました:for box 1 => [@boxScale] = "animate1" 'for box2' [@boxScale] = "animate2" ' – sTx

+0

あなたの助けを借りて私の配列でクラスオブジェクトではなく文字列をプッシュしていることを発見しました。* ngFor boxは私が最初に思ったように、クラスのオブジェクトではない文字列を表します。一方、 '* ngFor'の' this.animate1'を 'trueまたはfalse'(this.animate1の値)で' animate1'私が必要としているように – sTx

関連する問題