Angular - 5.6-Bind custom events
In the last few videos, we learned something crucial, something that we will use a lot in our demo application and this course, and probably in any application: how to transmit data from one component to another component that is implemented there. Now, in terms of the top management, what happens if we have a component and something changes and we want to inform our parent component, so the component that implements the other component.
For example, here in the application component, we implement the application cockpit and in this child component of the application cockpit, something can change because we have two buttons here and when we click on the buttons, we want to do something. And right now, the code that would normally be executed was simply put into comment. So we want to inform our parent component, the application component in this case, that a new server or roadmap has been created. In our application component, we may still want to implement these two methods. So just add them here.
However, I will adjust the names and say onServerAdded. Because these methods will only be executed after this button has been clicked. So not once the button is clicked, but afterwards. Once we’ve really finished creating the server, so to speak. So onServerAdded and onBlueprintAdded seem to be more appropriate names here in the application component.
However, we can comment on this code and still create a new server or roadmap. However, this will not work as expected here as we are referring to the new server name and new server content that are not available in the application component. But this is exactly the information we want to transmit from the cockpit to the application component. Because the application component is the central place in our application at the moment, which manages this series of services. How can we issue an event, or our own event? How can we inform our application component?
It would be great if it looked like this in the application cockpit. So, in the HTML file of the application component where we implement the application cockpit, it would be great if we could listen to serverCreated, any name you like but it would be a suitable name for the type of event we want to listen to. Of course, this event does not exist by default. So just as we listened to the click, now we want to listen to an event like this, serverCreated. And once our server has been created, we execute a certain code.
for each paragraph in HTML format:
Just like in any other built-in event like "Click", here on the right side of the equal sign between the quotes is a function that is triggered when the user clicks on the button.
This function is called "myFunction", but it can be named as you wish. It is important to keep in mind that the name must be unique and not have special spaces or characters.
Then, inside the function, we have a variable called "x" which contains the value of the HTML element with the "myText" ID.
This means that when the function is called, the value of the text in the HTML element with the "myText" ID will be stored in the variable "x".
Finally, the function changes the value of the HTML element with the "demo" ID using the "innerHTML" method. In this case, the value is "Hello World!".
However, you can change the value for what you want to display. You can also use a variable to store the value you want to display and use it instead of the "Hello World!" string.
In summary, this simple feature allows you to change the content of an HTML element when you click a button. This is a very useful technique for creating dynamic user interactions in your web projects.
Just like in any other embedded event like "Click", here on the right side of the equal sign between the quotes, there is a function that will be called when the event occurs. However, rather than having a separate function, we can use an anonymous function, or a nameless function, directly in the code. This function will be executed when the event occurs.
This type of function is often used when defining events on HTML elements, such as buttons or links. For example, if we wanted to call a function named "envoyerFormulaire()" when a user clicks on a button, we could use the following code:
<button onclick="envoyerFormulaire()">Envoyer</button>
However, we can also use an anonymous function to accomplish the same task:
<button onclick="function() { /* code to send form */ }">Envoyer</button>
This way, we don’t need to declare a separate function just to manage the event. We can simply use an anonymous function directly in the HTML code. This can be useful for simple functions that will only be used once in the code.
Anonymous functions can also be used in JavaScript to create dynamic functions that can be passed as arguments to other functions. For example, if we had a function called "effecterAction()", which takes a function as an argument and executes it, we could use an anonymous function to create a specific action to execute:
effectAction(function() { /* code to perform a specific action */ });
Using an anonymous function this way, we can create dynamic functions that can be used in different contexts, without having to define a separate function for each context.
In short, anonymous functions are nameless functions that can be used directly in code, often to manage events on HTML elements or to create dynamic JavaScript functions. They are particularly useful for simple functions that will only be used once in the code.
As a dynamic user in your web projects, it’s important to understand how and when to use anonymous features to improve the efficiency and clarity of your code.