I am new to angular2, I have 2 questions :-
1. I want to know why this is correct
<p myHighlight [color1]='color' defaultColor="violet">Highlight me!</p>
and these are not
<p myHighlight color1='color' defaultColor="violet">Highlight me!</p>
<p myHighlight [color1]='color' [defaultColor]="violet">Highlight me!</p>
Why i have to put only 1 property in brackets and the other without brackets.
2. How can i use @Input() directive without aliasing in my code. does it work for multiple properties ? Here is my directive code :
import { Input, Directive, ElementRef, HostListener } from '@angular/core';
@Directive({
selector: '[myHighlight]'
})
export class myDir {
constructor(private el: ElementRef) {
}
@Input('color1') color: string;//for ts code 'color' for html code 'myHighlight' i.e template component
@Input('defaultColor') defaultColor: string;
@HostListener('mouseenter') onMouseEnter() {
this.highlight(this.color || this.defaultColor);
}
@HostListener('mouseleave') onMouseLeave() {
this.highlight(null);
}
private highlight(color: string) {
this.el.nativeElement.style.backgroundColor = color;
}
}
my html code :-
<div>
<input type="radio" name="clr" (click)="color='lightgreen'">Green
<input type="radio" name="clr" (click)="color='yellow'">Yellow
<input type="radio" name="clr" (click)="color='cyan'">Cyan
</div>
<p myHighlight [color1]='color' [defaultColor]='violet'>Highlight me!</p>
Use the @Input() decorator in a child component or directive to let Angular know that a property in that component can receive its value from its parent component. It helps to remember that the data flow is from the perspective of the child component.
@Input Decorator. @Input is a decorator to mark a property as an input. @Input is used to define an input property, to achieve component property binding. @Inoput decorator is used to pass data (property binding) from parent to child component.
Input data into a Directive We have learned that directives in Angular are incredibly powerful. We can also extend or modify the behavior or functionality of a directive by inputtting data into the directive.
In Angular, a Directive is essentially a typescript class which has been annotated with a TypeScript Decorator. The decorator is the @ symbol. Decorators are not currently part of the JavaScript functionality (although they likely will be in the future) and are also still experimental in TypeScript.
First One
<p myHighlight color1='color' defaultColor="violet">Highlight me!</p>
color1 and default color are not attributes of paragraph tag so it will not work.
Second
<p myHighlight [color1]='color' [defaultColor]="violet">Highlight me!</p>
Your directive sets the color1 property to your paragraph tag and there is no violet variable in the component.
Everything inside [ ]
are property bindings and those properties are represented using @Input()
decorator.
Note: When you are using a component variable to bind property it should be in [ ]. If you are using direct value then you need not enclose in square brackets
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