While reading about @Input()
and @Output()
I have found that we can use an alias instead of using the property name for the decorators.
Example
class ProductImage {
//Aliased
@Input('myProduct') product: string;
//Un-aliased
@Input() product: string;//not aliased
}
HTML
//Aliased attribute
<SomeComponent [myProduct] = "Product Name"></SomeComponent>
//Un-aliased attribute
<SomeComponent [product] = "Product Name"></SomeComponent>
The official Angular documentation says:
Sometimes we want the public name of an input/output property to be different from the internal name. This is frequently the case with attribute directives. Directive consumers expect to bind to the name of the directive. For example, when we apply a directive with a myClick selector to a tag, we expect to bind to an event property that is also called myClick.
And This tutorial briefly explains it:
an alias let's me override the property name to be the alias instead of the original property name
Other than that I have not been able to find anything else on aliasing @Input()
and @Output()
on SO or through Google.
Things I would like to know are:
Path aliases simplify paths by giving a link to the path rather than using the the fully qualified path name. Using them will make your code easier to read and maintain. Path aliases are relative to compilerOptions.
Now, these two decorators have distinct functions: @Ouput - A decorator that lets the child component communicates with the parent component. @Input - A decorator that allows the parent to communicates with the child component.
Decorator that marks a class field as an input property and supplies configuration metadata. The input property is bound to a DOM property in the template. During change detection, Angular automatically updates the data property with the DOM property's value.
The above pattern — passing data in through an “input” property and sending data out through an “output” event — is the primary way to share data between Angular 2 components.
It's like your name and your nickname.
Inside your family you might be called Nic, where as , outside your family in order for other to know you legally , you should be called Nicolas.
So, if we consider the class of your component , the inside of your family :
@Component({
selector:'my-component'
})
export class MyComponent{
@Output('nicolas') nic = new EventEmitter<any>();
someFunction(){
// because we're inside the family ( inside this class ), instead of nicolas , we can call nic like this :
this.nic ...... ( which you'd normally emit an event ).
}
}
But from outside, you're still nicolas , so when someone want's to call you ( use that event emitter ) it should use nicolas;
<my-component (nicolas)="onNicolasChanged($event)"></my-component>
Note that we can't do this :
<my-component (nic)="onNicolasChanged($event)"></my-component>
This is aliasing that variable.
Also , please read my answer here :
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With