Angular 2 overriding components

I am from the world Laraveland a little new to the world Angular 2, and it’s hard for me to understand:

Is it possible to override a component or template in Angular 2, as we use to override provider / custom package views in Laravel?

This is a dummy folder structure that can express what I want to ask:

|-resources
   |-assets
      |-typescript
         |-core
            |-core.component.ts    //overridded core component and template
            |-core.template.html
|-Modules
   |-Core
      |-Resources
         |-assets
            |-typescript
               |-core
                  |-core.component.ts  //main module component and template
                  |-core.template.html

core.template.html (Original)

<div>
    <p> This is a core component template</p>
    <button>Click me! </button>
</div>

core.template.html (overridden)

<div>
    <p> This is a overridden core component template</p>
    <p> Removed the button and overridden with p-tag </p>
</div>

Hopefully I have clearly illustrated the problem I am facing.

+4
source share
1 answer

Overriding a component command element to an open problem with Angular 2

: https://github.com/angular/angular/issues/11144

Reflect .

import {Component} from 'angular2/core'

@Component({
  selector: 'thirdpartycomp',
  providers: [],
  template: `Hello From Third Party App!`,
  directives: []
})
export class ThirdParty{}

annotations = Reflect.getMetadata('annotations', Thing);
for (let i = 0; i < annotations.length; i += 1) {
  if (annotations[i].constructor.name === 'ComponentMetadata') {
    annotations[i].template = 'Hello From My App!';
    break;
  }
}

@Component({
  selector: 'my-app',
  directives: [ThirdParty],
  template: `<thirdpartycomp></thirdpartycomp>`,
})
export class App {}
0

Source: https://habr.com/ru/post/1661542/


All Articles