Issue with binding and @ViewChild

不问归期 提交于 2019-11-29 16:40:01
yurzui

Update 2.3.0

Great news!

According to angular blog http://angularjs.blogspot.ru/2016/12/angular-230-now-available.html

Developers can now take advantage of object inheritance for components. Share or simplify component functionality by inheriting from a parent component.

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 overrides propMetadata defined in the base Extended class. Hence your ExtendedInputBroken class doesn't have parent properties like baseLevel and baseLevelChange

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:

function InheritPropMetadata() {
  return (target: Function) => {
    const targetProps = Reflect.getMetadata('propMetadata', target);

    const parentTarget = Object.getPrototypeOf(target.prototype).constructor;
    const parentProps = Reflect.getMetadata('propMetadata', parentTarget);

    const mergedProps = Object.assign(targetProps, parentProps);

    Reflect.defineMetadata('propMetadata', mergedProps, target);
  };
};

And then apply it on your ExtendedInputBroken class:

@InheritPropMetadata()
export class ExtendedInputBroken extends Extended<string> implements OnInit {
  ...

Plunker demo

This link could interest you:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!