Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@Input and other decorators and inheritance

I don't really understand how object binding works, so if anyone could explain if I can use @Input() inside a base class, or better: decorators and inheritance. For example if each form should receive a customer I have a base class:

export class AbstractCustomerForm{

@Input() customer;
...
}

and then I extend this class in an actual component:

export AwesomeCustomerForm extends AbstractCustomerForm implements OnInit{
    ngOnInit(){

        if(this.customer)
            doSomething();

    }
}

but this won't work, customer will never get set :(

like image 967
Silencer Avatar asked Feb 25 '16 15:02

Silencer


People also ask

What is the difference between @input and @output in Angular?

@Input() and @Output() give a child component a way to communicate with its parent component. @Input() lets a parent component update data in the child component. Conversely, @Output() lets the child send data to a parent component.

What is the use of @input decorator?

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.

What is the use of @input and @output decorator in Angular?

@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. The component property should be annotated with @Input decorator to act as input property. Let's explore it practically.

What are decorators in Angular 2?

Decorators are a new feature of Typescript and used throughout the Angular 2 code, but they are nothing to be scared of. With decorators we can configure and customise our classes at design time. They are just functions that can be used to add meta-data, properties or functions to the thing they are attached to.


1 Answers

update

Inheritance is properly supported since 2.3.0-rc.0

original

Decorators are not inherited. They need to be applied to the class used as component directly. Decorators on subclasses are ignored. I have seen it mentioned that @Input() or @Output() are working if only the super class has them and the sub-class has none.

  • https://github.com/angular/angular/issues/5794
  • https://github.com/angular/angular/issues/5415
  • https://github.com/angular/angular/issues/7191
like image 125
Günter Zöchbauer Avatar answered Oct 03 '22 05:10

Günter Zöchbauer