
Angular 2 Input and Output is a powerful feature that allows components to communicate with each other. It's a crucial concept to grasp if you want to build complex and dynamic applications.
Input properties allow you to pass data from a parent component to a child component, making it easy to share data between components. In Angular 2, you can use the @Input() decorator to define an input property.
Input properties can be primitive types, such as strings or numbers, or complex objects. For example, you can pass a user object from a parent component to a child component using the @Input() decorator.
Output properties, on the other hand, allow you to pass data from a child component back to its parent component. You can use the @Output() decorator to define an output property.
Check this out: Css Selector the Last 2 Child Elements
Angular 2 Basics
In Angular 2, property binding is used instead of attribute binding, which means we access JavaScript properties rather than fetching data from existing HTML attributes.
Angular 1 uses attribute binding, but Angular 2 uses property binding. This is a different compile phase.
To bind an event in Angular 2, we use parentheses in the template syntax, as seen in the CounterComponent example.
Output Syntax and Usage
The basic syntax of @Output is straightforward: alias: the name of the DOM property, propertyName: the name of the property that emits events, and type: the generic parameter defining the data type of the event payload.
You can use the @Output decorator to mark a component property as an output of the component, allowing it to report output events to its parent components. This is a mechanism that enables event-driven communication between components.
The @Output() property becomes the name of the custom event by default, but you can use an alias to specify a different name. This is useful if you want to use a different name for the event binding.
There are two ways to use the @Output decorator: as a decorator on a class field, or as an outputs property inside the @Component() decorator. Both methods achieve the same result.
One-way binding from a parent component to a nested component uses square brackets, [propertyName], while one-way binding from a nested component to a parent component uses parentheses, (propertyName). Two-way binding uses banana box notation, [(propertyName)].
You might enjoy: HTC One Mini 2
The @Output() decorator must be associated with an EventEmitter, which emits an event when called. Any parent component listening to that event through an event binding can respond to it accordingly.
Here's a quick reference to the different types of event bindings:
Input and Output in Directives
Directives in Angular 2 have two ways to pass in data: scope or bindToController. We can either use the bindToController property and specify an object of bindings, or use the scope property to declare the bindings and alternative bindToController syntax.
In directives, we can use the @Input() decorator to bind properties from the parent component to the directive, and the @Output() decorator to emit events from the directive to the parent component. For example, we can use the counter Directive and demonstrate input bindings through accessing the count attribute via bindToController.
Here are some ways to pass data to directives:
- One-way binding from parent component to nested component: `[propertyName]`
- One-way binding from nested component to parent component: `(propertyName)`
- Two-way binding (a.k.a banana box notation): `[(propertyName)]`
Note that the @Input() and @Output() decorators are used to define inputs and outputs for components, and can be used in a similar way in directives to bind properties and emit events.
Avoid Common Output Mistake
A common mistake when using @Output is to use it to explicitly trigger actions in other components, which can lead to code that's hard to maintain.
The @Output mechanism is meant to report custom events, not trigger actions. The difference is subtle, but crucial to understand.
Event and command are two different concepts that should not be mixed together. An event reports an internal change of state of a component, while a command tells another component to perform a specific action.
Notice the name of the output: it's not an event, but a command. This output tells some other external component to perform an explicit action.
The problem with this design is that the child component has too much information about the internal workings of another component. The business logic is likely at the wrong place in the application.
Here's a simple way to remember the difference:
By using an event output instead of a command, the child component is not aware of what other components are interested in the event, and it doesn't make assumptions about how, where, when, or if the event will be used.
This way, the child component makes no assumptions about how, where, when, or if the custom event will be used. It's up to the consumers of this output to decide what to do in response to the event.
Notice the placement of the business logic: it's now in the right place. The child component should not be aware of the internal workings of other components.
Alternative Syntax
If you're looking for alternative syntax for using @Input() and @Output() in your directives, you're in luck. There are a few ways to do this.
You can use the alternative syntax for @Input() by using it as an inputs property inside the @Component() decorator, but this is not the most favored approach.
Alternatively, you can use @Output() as an outputs property inside the @Component() decorator, but this is not typically advised either.
Here's a summary of the alternative syntax options:
These alternative syntax options can be useful in certain situations, but it's generally best to stick with using TypeScript decorators for @Input() and @Output() to keep things string-less and dynamic.
Directive Attribute Bindings
Directives offer two ways to pass in data: scope or bindToController. The bindToController property allows us to specify an object of bindings.
In directives, we can use the bindToController property to make properties available in the template and controller for manipulation. This is demonstrated in a simple counter Directive where the count property is specified as an input binding.
We can also use the scope property to declare the bindings and alternative bindToController syntax. Both methods achieve the same goal of making the count property available for manipulation.
In Angular 1.x, we can use the bindings syntax to change the binding name to a different internal mapping. For example, we can use bindings: { foo: '
Angular 2's @Input() decorator can also be used to define the name of the input binding. We can pass a string into the decorator to specify the name of the binding. This is typically done by using a TypeScript decorator to keep things string-less and dynamic.
In Angular 1.x, we can also use the bindings syntax to change the binding name for output bindings. For example, we can use bindings: { foo: '&bar' } to change the binding name from bar to foo.
Expand your knowledge: 3 1 2 Minutes
Input Event Overview and Usage
The "input" event is a native JavaScript DOM event that is triggered as the user interacts with text-based input controls.
It's a synchronous event, meaning it's fired immediately after any mutation of the value property, whether that's through user key-stroke events or user paste events.
The "input" event has cross-browser support for text-based Input and Textarea elements, but only partial support for Select elements.
For Select elements, you're better off listening for the "change" event. Checkboxes, while inputs, don't seem to fire the "input" event, so you should use "change" for checkboxes as well.
The "input" event is fired with every change the user makes to the input, whereas the "change" event is only fired at the end once the form field is blurred.
This makes the "input" event a more elegant solution than trying to listen for key-events and click-events to capture all input mutations.
Additional reading: Css Input Text
Asynchronous Data
Asynchronous Data is a common challenge when working with Angular 2 inputs.
You can use ngOnChanges to detect changes in a component's inputs and wait until they are defined before acting upon them.
If a child component tries to use data before it's received, it will throw an error.
The child component must wait for the data to exist before using it, so you use ngOnChanges to check if the data exists.
The template for the child component will not show if a property that relies on the data being passed in is not true.
You can use ngOnChanges to detect changes in a component's inputs and wait until they are defined before acting upon them.
This approach is especially useful when working with asynchronous data that hasn't been received yet.
Check this out: What Are the Most Important Inputs to a Unit Forecast
Example and Final Code
In Angular 2, you can bind data between components using the @Input() and @Output() decorators.
The @Output() decorator is useful for updating the parent component when the child component's state changes. You can see this in action in the final code example, where incrementing or decrementing the counter updates the parent through the @Output() event.
To use the @Input() decorator, you first need to import it in your component, then add it as a property of your component class. For instance, if your component selector is 'car-component', you can call it with the attribute 'car' and access the data as an attribute in your object (this.car).
The final code example shows how the @Output() decorator works in practice, updating the parent component when the child component's state changes.
Nested Components and Syntax
Nested components can be used to create complex UI elements by accepting inputs and emitting outputs.
A Button directive can be created to accept an @Input() to specify a click limit until the button gets disabled.
This allows the parent component to listen to an event which will be emitted when the click limit is reached via @Output.
The parent component can then alert a message when the click limit is reached.
The Button directive and parent component can be used together to create a functional and interactive UI element.
By using @Input() and @Output() in a nested component, developers can create reusable and modular code that is easy to maintain and update.
See what others are reading: When Will Destiny 2 Come Back Online
Featured Images: pexels.com


