This plnker is probably the quickest way to see the issue
I'm not sure if it's just some obvious gotcha when using ViewChild but it's very strange.
The plunker is showing 3 inputs:
- The first input is a basic input and a button that can focus it.
- The second input is bound to the same value, and clicking edit will unlock the input.
- The third input is also bound to the same value, and clicking edit will unlock the input and give it focus.
However, when adding the ViewChild to get a reference to the input, the NgModel binding on the input stops working. But any other bindings you attach (like disabled) will still continue to function. If you comment out line 52 of app/extended.component it will bind again, but obviously now it can't focus.
The first input/button shows that this is apparently only an issue when you're binding to a property from the class you're extending.
In short, when I access the input via ViewChild my binding to NgModel breaks.
That is, given a base with property "someValue": Binds:
@Component({
selector: 'binding-working',
template: `<input type="text" [(ngModel)]="someValue" />`
})
export class Working extends Base<string> {
constructor() { }
};
Doesn't bind:
@Component({
selector: 'binding-broken',
template: `<input type="text" #imBroken [(ngModel)]="someValue" />`
})
export class Broken extends Base<string> {
@ViewChild('imBroken') input;
constructor() { }
};
Update 2.3.0
Great news!
According to angular blog http://angularjs.blogspot.ru/2016/12/angular-230-now-available.html
So then it should work without custom decorator => Plunker Example
More details you can see in this commit https://github.com/angular/angular/commit/f5c8e0989d85bc064f689fc3595207dfb29413f4
Old version
That's as designed.
Angular2 doesn't support the full inheritance (https://github.com/angular/angular/issues/7968#issuecomment-219865739).
Your child
ViewChild
decorator overridespropMetadata
defined in the baseExtended
class. Hence yourExtendedInputBroken
class doesn't have parent properties likebaseLevel
andbaseLevelChange
Thierry Templier wrote a great article about class inheritance in Angular2
If you really want to do that i can offer you the following solution.
Just create custom decorator like this:
And then apply it on your
ExtendedInputBroken
class:Plunker demo
This link could interest you: