Leveraging animations in Angular2 that are defined outside of a component

I've recently put together a basic component

@Component({
  selector: 'saved-overlay',
  templateUrl: './saved-overlay.html',
  exportAs: 'saved-overlay',
  animations: [
    trigger('inOut', [
      transition('void => *', [
          animate("300ms ease-out", keyframes([
            style({opacity: 0}),
            style({opacity: 1})
          ]))
        ]),
        transition('* => void', [
          animate("300ms ease-out", keyframes([
            style({opacity: 1}),
            style({opacity: 0})
          ]))
        ])
      ]
     })
   })       
export class SavedOverlayComponent {
}

Now, when I include it in my template:

<saved-overlay #myOverlay='saved-overlay'></saved-overlay>

I'm wondering if there is a way to activate the inOut animation from an external source (for example, within the referencing template). Ideally, I'd like to apply this animation directly on the component like so:

<saved-overlay #myOverlay='saved-overlay' [@inOut]></saved-overlay>

However, this approach triggers an error indicating that the inOut animation is not defined.

Answer №1

To achieve this functionality, you can utilize @HostBinding:

@Component({
  selector: 'saved-overlay',
  templateUrl: './saved-overlay.html',
  exportAs: 'saved-overlay',
  animations: [
    trigger('inOut', [
      transition('void => *', [
        animate("300ms ease-out", keyframes([
          style({opacity: 0}),
          style({opacity: 1})
        ]))
      ]),
      transition('* => void', [
        animate("300ms ease-out", keyframes([
          style({opacity: 1}),
          style({opacity: 0})
        ]))
      ])
    ])
  ]
})
export class SavedOverlayComponent {
  @HostBinding("@InOut")
  customProperty:any;
}

There is no necessity to bind anything or specify it in the template :

<saved-overlay #myOverlay='saved-overlay'></saved-overlay>

Remember that any random property can be used because the content stored inside this property is not crucial. While utilizing the special states (* and void), the specific name of the property does not matter...

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Developing interconnected dropdowns in Angular 8 for input fields

Imagine we have a list of names structured like this: nameSelected: string; names: Name[ {firstName: 'John', middleName: 'Danny', lastName: 'Smith'}, {firstName: 'Bob', middleName: 'Chris', lastN ...

Are you interested in creating dynamic tables/models with Sequelize?

Currently, I am exploring a theoretical question before diving into the implementation phase. The scenario is as follows: In my application, users have the ability to upload structured data such as Excel, CSV files, and more. Based on specific requirement ...

Expressions without a call signature cannot be invoked

When using an adapter in the given example, I encountered a type error specifically on the last line of the getGloryOfAnimal method. Despite having clearly defined types, I am puzzled by this issue. interface ICheetah { pace: string; } interface ILio ...

The issue arises in React when input elements fail to render correctly following a change in value, specifically when the keys remain identical

Click here to view the code sandbox showcasing the issue The code sandbox demonstrates two versions - a working one where Math.random() is used as the key, and a not working one where the index of the array is used as the key. When the array this.state.v ...

Issue code: 135 - Resolution steps: You are encountering an issue as the update-config.json file is missing. To resolve this, execute the command 'webdriver-manager update

[17:05:22] I/launcher – Running 1 instances of WebDriver [17:05:22] I/direct – Using ChromeDriver directly… [17:05:22] E/direct – Error code: 135 [17:05:22] E/direct – Error message: Could not locate update-config.json. Please run ‘webdriver ...

There seems to be an issue with the useReducer value not updating when logging it in a handleSubmit function

I'm currently incorporating useReducer into my Login and Register form. Interestingly, when I attempt to log the reducer value, it only displays the default value. However, if I log it within the useEffect hook, it functions correctly. Below is a sn ...

Unique icons crafted for iOS and Android devices

I have unique custom icons named "md-usb.svg" for both iOS and Android. After copying the Android icon to the designated folder: android\app\src\main\assets\public\svg When searching for the standard icon md-bluetooth.svg, ...

The web server is now serving Index.html instead of main.js for AngularJS 2

Transitioning from an angular1 application to an angular2 one, I encountered an error after removing the ng-app directive and adding the system.config: Error: SyntaxError: Unexpected token < Evaluating https://localhost:8080/contents/app/main.js Error ...

Adding a class to an element in Angular 6 using Renderer2/3 - a simple guide

//Parent Component html: <div class="modal" id="modal" data-backdrop="static" data-keyboard="false"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-body"> <router-o ...

The HTTP post method in Angular 2 fails to properly send data to request.JSON within a Grails Action

Having trouble retrieving data from request.JSON passed through an Angular 2 HTTP POST method. The Grails action is being triggered, but the request.JSON is consistently empty {} even when data is passed. ANGULAR2: HTTP POST Method: return this.http.pos ...

I encountered a warning while using the useViewportScroll in NextJs with Framer Motion: "Caution: The useLayoutEffect function does not have any effect on the server

Successfully implementing NextJs with Framer Motion, yet encountered a warning: Warning: useLayoutEffect does not function on the server due to its effect not being able to be encoded in the server renderer's output format. This may cause a differenc ...

"String representation" compared to the method toString()

Currently, I am in the process of writing unit tests using jasmine. During this process, I encountered an issue with the following code snippet: let arg0: string = http.put.calls.argsFor(0) as string; if(arg0.search(...) This resulted in an error stating ...

What could be causing the "Permission Denied" error to occur when attempting to post data to Firebase realtime database using Angular?

I've been working on a bookshop application using Angular 9.0 and Firebase Realtime Database. So far, I've been able to handle writing and reading data with the default rules. { "rules":{ ".read": true, ".write": true } } No ...

Angular observable goes unnoticed

I'm venturing into creating my own observable for the first time, and I'm running into issues. The data is visible in the console, but it doesn't show up when the component loads. Can anyone help me troubleshoot this problem? Here's a ...

Transferring information from childA component through its parent component and into childB component

In my project, there is a main parent component with two child components: 1. app-search-list and 2. app-vertical-menu The data is passed from the app-search-list (childA) component to its parent component, and then from the parent to the app-vertical-men ...

The template reference variable has not been defined

The issue I'm facing is related to the template reference variable #newSkill on an input field. When passed as a parameter in the addToSkill() method, it works perfectly fine. However, when used in the chooseSkill() method triggered by clicking list ...

Troubleshooting issues encountered while sending HttpClient Get Requests from Angular Front-End to Node.js Back-End

I am encountering an issue where I keep getting the error message "Cannot GET /" on my local host URL when attempting to send a HttpClient get request from my Angular front-end to my Node.js back-end endpoint. Despite the fact that my back-end endpoint suc ...

What is the proper way to utilize setTimeout in TypeScript?

Let's take a look at an example of how to use setTimeout in Angular and TypeScript: let timer: number = setTimeout(() => { }, 2000); However, upon compilation, you may encounter the following error message: Error TS2322: Type 'Timeout' ...

Unable to access /route upon refreshing page in Angular 7

After developing several components in Angular 7, I decided not to use 'useHash: true' for routing. Everything seemed to be running smoothly when I deployed my Angular app on a live server. However, I encountered an issue when reloading a page a ...

What is the best way to activate an alert or swal function just once instead of repeatedly?

I am just starting to learn Angular. Currently, I have an application that contains two variables related to the status of financial transactions. These variables are: tab1TrxMessage, which holds any important messages, and tab1TrxStatus that indicates wh ...