2016-08-20 38 views
1

我已经检查以下角2动画文档:https://angular.io/docs/ts/latest/guide/animations.html角2有生

,并开始与它玩。我去了最简单的例子,并做了以下内容:

添加到我的组件:

animations: [ 
    trigger('dataState', [ 
     state('inactive', style({ 
     backgroundColor: '#eee', 
     transform: 'scale(1)' 
     })), 
     state('active', style({ 
     backgroundColor: '#cfd8dc', 
     transform: 'scale(1.1)' 
     })), 
     transition('inactive => active', animate('100ms ease-in')), 
     transition('active => inactive', animate('100ms ease-out')) 
    ]) 
    ] 

添加到我的模板:

<div class="well" 
[@dataState]="data.active" 
(click)="data.toggleState()" style="cursor:pointer"> 
SOME TEXT 
</div> 

当然进口的一切..

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

但是当我点击我的对象时,我得到:

browser_adapter.js:86 TypeError: self.context.$implicit.toggleState is not a function 

由于toggleState是不是在我试图将其删除的文档,但毕竟是没有动画的效果(虽然没有错误)

什么我失踪?

回答

2

您在示例中通过[@dataState]="data.active"分配给@dataState的值可能是'active''inactive'。 (好吧,价值可能会不同,但是除非你指定它,否则不会有任何影响)

所以你的component.ts需要一个属性data.active,该属性的值为一。是什么触发动画是它们之间的变化:

[@dataState]="'active'"[@dataState]="'inactive'"反之亦然

data.active仅仅是一个变量,data.toggleState()是分配'active''inactive'它的功能。

1

我也遇到过这个问题。我发现大多数文档都会引导您完成所需的每个步骤,但在此处跳过添加一些设置。我猜他们认为在文档中的这一点上有几件事情是不言自明的。

如果你看看live plunker example并查看代码,你可以看到他们是如何设置它的。

在app/hero.service.ts中,他们添加了一个构造函数,它向Hero类中添加了一个hero.state字符串和一个toggleState()方法。根据你的错误

class Hero { 
    constructor(public name: string, 
       public state = 'inactive') { 
    } 

    toggleState() { 
    this.state = (this.state === 'active' ? 'inactive' : 'active'); 
    } 
} 

和质疑,似乎你还没有加入中的任一这些又是为什么toggleState()不是一个函数,没有什么可以没有国家的属性进行切换。

为了您例如,你可以(根据您的类)做这样的事情:

import { Component, Input, trigger, state, style, transition, animate } from '@angular/core'; 
@Component({ 
    selector: 'basic-component', 
    template: ` 
    <div class="well" [@dataState]="active" (click)="toggleState()" style="cursor:pointer">SOME TEXT</div> 
    `, 
    animations: [ 
    trigger('dataState', [ 
     state('inactive', style({ 
     backgroundColor: '#eee', 
     transform: 'scale(1)' 
     })), 
     state('active', style({ 
     backgroundColor: '#cfd8dc', 
     transform: 'scale(1.5)' 
     })), 
     transition('inactive => active', animate('100ms ease-in')), 
     transition('active => inactive', animate('100ms ease-out')) 
    ]) 
    ] 
}) 
export class BasicComponent { 
    active = 'inactive'; 
    toggleState() { 
    this.state = (this.state === 'active' ? 'inactive' : 'active'); 
    } 
} 
1

我还发现angular.io动画文档是不完整的。我使用了示例代码并做了一些小改动,使其可以使用TOH app tutorial中的代码。

TL; DR

一个state="inactive"属性添加到hero.ts英雄类跟踪每个主人公的动画状态。

在HTML,改变他们的(click)="hero.toggleState()"方法结合(click)="toggleState(hero)"和编写方法在HeroesComponent类:

toggleState(hero: Hero) { hero.state = (hero.state === 'active' ? 'inactive' : 'active'); }

重新布线onSelect()方法,以便gotoDetail()导航作品。

Working plunk that follows steps below

Here is the plunker they provide with everything complete through section 5 - routing.使用它跟着一起,如果你愿意的话。

我将介绍如何修改该plunk以在其动画文档中实现第一个动画。

它们部分步行通过在动画文档的第一个代码是动画活动/非活动状态添加到选定的英雄的英雄视图(相对于仪表板视图):

import { Component, Input, trigger, state, animate } from '@angular/core'; 
import { Heroes } from './hero.service'; 
@Component({ 
    moduleId: module.id, 
    selector: 'hero-list-basic', 
    template: ` 
    <ul> 
     <li *ngFor="let hero of heroes" 
      [@heroState]="hero.state" 
      (click)="hero.toggleState()"> 
     {{hero.name}} 
     </li> 
    </ul> 
    `, 
    styleUrls: ['hero-list.component.css'], 
    animations: [ 
    trigger('heroState', [ 
     state('inactive', style({ 
     backgroundColor: '#eee', 
     transform: 'scale(1)' 
     })), 
     state('active', style({ 
     backgroundColor: '#cfd8dc', 
     transform: 'scale(1.1)' 
     })), 
     transition('inactive => active', animate('100ms ease-in')), 
     transition('active => inactive', animate('100ms ease-out')) 
    ]) 
    ] 
}) 
export class HeroListBasicComponent { 
    @Input() heroes: Heroes; 
} 

以上,其(动画示例)代码与app/heroes.component.ts(来自plnkr)中的代码相似,并注意到已将html/css解压到单独的文件中。我认为大部分阅读过的人都遵循了教程,并且熟悉了这段代码。

heroes.component.html 新的动画将基本复制在每个英雄<li>现有的绑定,所以删除这两条线 - 因为如果我们让他们,他们会冲突 - 我们要带来的是功能回到动画状态。从动画例如

<ul class="heroes"> 
    <li *ngFor="let hero of heroes"> 

    --------->[class.selected]="hero === selectedHero" 
    --------->(click)="onSelect(hero)"> 

    <span class="badge">{{hero.id}}</span> {{hero.name}} 
    </li> 
</ul> 

新的HTML:

<ul class="heroes"> 
    <li *ngFor="let hero of heroes" 
    [@heroState]="hero.state" 
    (click)="hero.toggleState()"> 
    <span class="badge">{{hero.id}}</span> {{hero.name}} 
    </li> 
</ul> 

我不想给toggleState方法添加到英雄职业,我希望它在调用它的分量。所以我改变了点击结合

(click)="toggleState(hero)" 

,只需点击的英雄传递给我们仍然需要编写的方法。

一个hero没有财产state但让我们补充一点,在应用程序/ hero.ts

添加state:string = "inactive";到属性列表。

现在让我们回到heroes.component.ts,导入我们动画的依赖,加上在@Component动画的元数据,并创建toggleState()方法。我们希望保留我们从html中删除的onSelect()方法,我们将稍后修改并重用它。

向上顶,更换

import { Component, OnInit } from '@angular/core';

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

追加animations元后styleUrls: [ ... ],

animations: [ 
    trigger('heroState', [ 
    state('inactive', style({ 
     backgroundColor: '#eee', 
     transform: 'scale(1)' 
    })), 
    state('active', style({ 
     backgroundColor: '#cfd8dc', 
     transform: 'scale(1.1)' 
    })), 
    transition('inactive => active', animate('100ms ease-in')), 
    transition('active => inactive', animate('100ms ease-out')) 
    ]) 
] 

在HeroesComp onent类添加以下方法:

toggleState(hero: Hero) { 
    hero.state = (hero.state === 'active' ? 'inactive' : 'active'); 
} 

这样所有的作品。现在让我们来破解英雄的细节。英雄详细信息在列表后显示了一个简略的英文单词,该英文单词显示选择了哪个英雄,并伴随着一个导航到detail /:id路由的按钮。但现在它消失了。我们分离的onSelect()方法正在启动该方法。

让我们重命名onSelect()updateSelectedHero(),然后从里面toggleState()称之为:

updateSelectedHero(hero: Hero): void { 
    this.selectedHero = hero; 
    } 

    toggleState(hero: Hero) { 
    hero.state = (hero.state === 'active' ? 'inactive' : 'active'); 
    this.updateSelectedHero(hero); 
    } 

AAAND我们回到业务。英雄细节显示出来,它的查看细节按钮调用gotoDetail()。有些讨厌的UI缺陷需要解决,但你明白了。

My finished plunk