Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to override directive definitions in angular 2

Tags:

angular

It is possible in angular 1 to decorate (override) directive definitions.

This is explained here: http://www.bennadel.com/blog/2926-overriding-directive-definitions-in-angularjs.htm

There is a builtin function

angular.module( "X" ).decorator(

for this.

In angular2 we don't have modules. The recommended way is to use typescript modules.

How will it be possible to decorate(override) directives in angular2?

The main reason i want to do this, is for customizations, when i deploy my app in a couple of sites.

Let's say that i have a bundle.js with all my app, I want then just to drop in a customer.js with a bunch of customizations, instead of changing and rebuilding my existing bundle.js for every site deployment.

Let's add a concrete example from the tour of heroes: I defined my app component in a javascript file:

import {Component, OnInit} from 'angular2/core';
import {Hero} from './hero';
import {HeroDetailComponent} from './hero-detail.component';
import {HeroService} from './hero.service';

@Component({
  selector: 'my-app',
  template:`
    <h1>{{title}}</h1>
    <h2>My Heroes</h2>
    <ul class="heroes">
      <li *ngFor="#hero of heroes"
        [class.selected]="hero === selectedHero"
        (click)="onSelect(hero)">
        <span class="badge">{{hero.id}}</span> {{hero.name}}
      </li>
    </ul>
    <my-hero-detail [hero]="selectedHero"></my-hero-detail>
  `,

  directives: [HeroDetailComponent],
  providers: [HeroService]
})

I want to be able in another javascript file to switch the HeroDetailComponent with another component CustomHeroDetailComponent.

How is this possible?

like image 578
David Michael Gang Avatar asked Jan 28 '16 08:01

David Michael Gang


People also ask

What is the difference between directive and component in angular 2?

The Component is used to break up the application into smaller components. That is why components are widely used in later versions of Angular to make things easy and build a total component-based model. The Directive is used to design reusable components, which are more behavior-oriented.

What is override in Angular?

Overriding dependencies in Angular requires two key properties: provide — this points to the dependency that you wish to override. useClass — this points to the new dependency, which will override the existing dependency in the providers property.

What are directives in angular 2?

A directive is a custom HTML element that is used to extend the power of HTML. Angular 2 has the following directives that get called as part of the BrowserModule module. If you view the app. module.

What is @directive in Angular?

What is meant by directives in Angular? Directives are classes that add new behavior or modify the existing behavior to the elements in the template. Basically directives are used to manipulate the DOM, for example adding/removing the element from DOM or changing the appearance of the DOM elements.


1 Answers

There is no direct support for this AFAIK. I guess the underlaying issue is the same as https://github.com/angular/angular/issues/5622

As a possible workaround, you can create a file that exports all your components and in the component source file you import from there; Then you can change the bindings by replacing this exporting file.

It is ugly because all components start depending on that global file.

like image 179
Günter Zöchbauer Avatar answered Sep 30 '22 23:09

Günter Zöchbauer