Assume this simple Angular 2 app
Main component:
@Component({
selector: "app"
})
@View({
directives: [ChildComponent]
template: '<child-component></child-component>'
})
export class App {}
Child component
@Component({
selector: "child-component",
template: '<input type="text" id="applyEverythingToMe" />'
})
export class ChildComponent {}
How can we apply any property binding, directive, whatsoever to the applyEverythingToMe
input field, without defining everything via @Attribute
, ... in the ChildComponent?
Assume e.g. we want to disable the input via
<child-component [disabled]="true"></child-component>
The plunk: https://plnkr.co/edit/kndYdGFsp8sEzCPpcKdq?p=preview
Edit:
As the first answers missed our actual problem, here is a bit more background:
We are using <input type="date" />
and split up the banana-in-the-box into () and [] (e.g. described here: https://stackoverflow.com/a/39890184/1256072).
This of course clutters every component with the parseDate, and it is also annoying that we cannot use [(ngModel)]
.
As we did not find any way to extend the ngModel
directive to e.g. myModelDate
which automatically splits [()]
and applies the parse method, we created our own component which implements ControlValueAccessor
so we can bind it with [(ngModel)]
. This components template only contains this one input type=date field. We now want our own component to behave just like any other input, so we can simply use <my-date [disabled]="true"/>
or <my-date [attr.whatever]="something" />
without explicitly defining all attributes via @Input
, @Attribute
, ...