2017-02-06 4 views
1

기본적으로 조건이 충족되면 특정 상자에 애니메이션을 적용 할 수 있기를 원합니다. 내 응용 프로그램에서는 사용자가 question1 (Component1) -> 공유 서비스 -> COmponent2에서 답변을 주었을 때 알림이 표시되고 답변이 올바른 경우 id = 1 인 상자 크기를 조정합니다 (나는 상자의 특이성의 중요성을 지적하기 위해 이드를 사용했다.)Angular2 애니메이션 [@triggerName]이 (가) * ngFor에서 작동하지 않습니다.

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

무엇 나는 수행하려고하는 [@boxScale]에 대한 올바른 값 ngfor에 추가하는 것입니다 그래서 결과는 다음과 같아야합니다

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

먼저 두 개의 하드 요소가 작동하지 않습니다 *ngFor으로 생성 된 animate1animate2 변수 만 다른 두, 함께 잘 작동 조금도.

내가 뭘 잘못하고 있니?

+0

상자를 확인하십시오 : 문자열 [] = []; 당신은 부울 배열이 필요합니다. 생성자에서 – MMK

+0

이 보이는 것 같습니다 - 내 상자 배열은 다음과 같습니다 : [ "animate1", "animate2"] – sTx

+0

Augury를 사용하고 배열이 괜찮습니다 – 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 값이 true가 아니라'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'을 가진 상자 (상자의 특이성을 중요시하기 위해 이드를 사용했습니다) – 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