Angular2: How to override components template?

谁说胖子不能爱 提交于 2019-12-04 18:23:30

问题


I am considering migrating an angular 1.4 application to angular 2 and I wonder if it will be possible to override components' template like we do in angular1 using $provide.decorator (like Can you override specific templates in AngularUI Bootstrap?).

I am looking for something like TestComponentBuilder.overrideTemplate but for a non-testing scenario. Does Angular2 comes with something similar?


回答1:


Check out this answer from stackoverflow Override/extend third-party component's template. The main idea is you can write your own component and extend third party component.

import {component} from 'angular2/core';
import {thirdPartyClass} from 'example/example';

@Component({
  selector: 'my-selector',
  template: '<div>my template</div>'
})

export class MyOwnComponent extends thirdPartyClass {
  constructor() {
     super()
  }
}

But there are downsides :

  1. It will still compile original component.
  2. If you are using this method, don't forget to import any pipes that are used in the thirdPartyClass template.
  3. If the functionality is updated in the thirdPartyClass that depends upon the template, you'll need to update by hand.

    Subscribe to this Github Issue for further updates.



来源:https://stackoverflow.com/questions/36666510/angular2-how-to-override-components-template

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