What is the main difference between the @Input () and @Attribute () decorators in Angular2?

I saw that @Attribute () is used in directives usually as a parameter in the constructor, like this:

export class EqualValidator implements Validator { constructor( @Attribute('validateEqual') public validateEqual: string) {} validate(c: AbstractControl): { [key: string]: any } {} } 

and @Input () used in such components:

 export class UserProfile { @Input() user; constructor() {} } 

And then you can pass data to these variables in the template with property binding in the case of @Input ().

What are the main differences between these decorators and when should you use them?

+7
angular
source share
1 answer

@Input (): used to pass values ​​to a directive or to transfer data from one component to another (usually from parent to child).

@Attribute (): you can get a constant value of the attribute available in the main element of the component / directive, and it must be used with the parameter of the component constructor or directives

Hope this help!

+4
source share

All Articles