angular: Angular 2 RC 4 New Animations not supported in Directives
Hi,
It seems that in current release we can add animations to components only and cannot define them as directives. I mean:
the below works
@Component({
selector: "animate-demo",
animations: [
trigger('openClose', [
state('closed, void',
style({ height: "0px" })),
state('open',
style({ height: "*" })),
transition("* => *", animate(200))
])
]
})
Whereas the below doesn’t work:
@Directive({
selector: "animate-demo",
animations: [
trigger('openClose', [
state('closed, void',
style({ height: "0px" })),
state('open',
style({ height: "*" })),
transition("* => *", animate(200))
])
]
})
And gives compilation error in visual studio:
"Argument of type ‘{ selector: string; animations: AnimationEntryMetadata[]; template: string; }’ is not assignable to parameter of type ‘{ selector?: string; inputs?: string[]; outputs?: string[]; properties?: string[]; events?: strin…’.
"
Usually we might want to have the commonly used animations such as slideDown, sliddeUp, fadeIn, fadeOut defined at one place as a directive and then in components where we want to use them we would just want to import the directive and use the animation instead of redefining animation in each component.
About this issue
- Original URL
- State: open
- Created 8 years ago
- Reactions: 71
- Comments: 33 (12 by maintainers)
Even though directives are not attached to a view, they should still be able to place animations on the host element that they are attached to.
Say for example you wanted to create a directive to manipulate
animate.css
animations:(The use of using CSS classes/keyframes within
animate
will be possible once the CSS parser is integrated. The example above is just to show how useful it can be to make a directive that manages animations.)As a workaround you can create
animations.ts
file like:And then use it in components like:
@matsko Been a few months since your last update. Any news?
@matsko can you please share if there is any progress on this?
Thank you so much Eric fro your reply. Then whats the best way if we want to make reusable animations? Apparently it seems that if n of my components requires slide up/down animation, I will have to write the same animation n times i.e. in each component, correct?
I believe the ability to define animations as structural directive would embrace re-usability of animations, what do you think?
Yes there is a plan in the works. I will update this thread once I get a go-ahead on this.
+1
@matsko Hasn’t been an update in awhile. Was wondering if this was still in the works?
Ok, I have managed to do animation using a directive. Here is an example:
and then use it like this :
I have a suggestion for the angular team regarding animation. I wonder if it is possible to introduce a new way to manage animation similar to the way angular is currently managing Components and Directives; I mean through dependency injection.
Something like:
then used in templates like:
I did something around that @molcik mentioned above.
Gist here: https://gist.github.com/McQuinTrix/3bd041a4ffefc53633d38d927c782db7
Edit: This is just basic implementation, people can figure out how to expand on this
Thank you so much @matsko for your reply, his looks really exciting. Can you please also create a Plunker for this?
hi is there any news on this feature? it is planned for a future release?
@fayezmm I had to eventually ditch the directive approach, it cannot bind to the animation on
enableProdMode()
like what it does in the angular-cli.However, animating with a reusable component is not difficult to put together, even if it is illogical from a syntax, structure, and philosophy point of view. Here’s what I did to get my reusable collapsing working, even in prod mode!
@matsko - Trying this on 2.0.1 and getting an error.
which gives me:
of coursing switching this to a
Component
and it works fine.Would this happen soon? I have been writing lots of components with similar animation applied. Directive is so desirable to make the code dry.