Remove/replace a component's selector tag from HTML [duplicate]
Asked Answered
B

2

78

I am getting started with the Angular 2 (version 2.0.0-alpha.46) and creating a few components.

When creating the component with the below code:

Typescript:

import {ComponentMetadata as Component, ViewMetadata as View} from 'angular2/angular2';

@Component({
   selector: 'my-component'
})

@View({
     template: '<div class="myClass">Hello My component</div>'
 })

export class MyCompoent{
    constructor() {
        console.info('My Component Mounted Successfully');
    }
}

HTML:

<my-component></my-component>

It works fine, but when i do Inspect element, I can see a tag generated like this:

Output HTML

<my-component>
    <div>Hello My component</div>
<my-component>

Problem

it keeps the <my-component> tag in the HTML, and some of my CSS are not working as expected.

Question

Is there a way to remove the <my-component> tag similar to angular 1 (replace: true in the directive)?

Bargain answered 4/1, 2016 at 10:20 Comment(2)
Just FYI, this has been asked at least 3 times already: 1, 2, 3.Churlish
The duplicates simply highlight the various scenarios this feature is required in. As this question points out, the CSS libraries don't work as expected with Angular because of this.Ob
C
68

Replace was deprecated in AngularJS 1.x according to https://github.com/angular/angular/issues/3866 because it seemed to not be a good idea.

As a workaround you can use an attribute selector in your component like

selector: '[my-component]'

selector: '[myComponent]'

and then use it like

<div my-component>Hello My component</div>

<div myComponent>Hello My component</div>

hint

Directive selectors should be camelCase instead of snake-case. Snake-case is only used for element selectors, because the - is required for custom elements. Angular doesn't depend on components being custom elements, but it's considered good practice to comply with this rule anyway. Angular works fine with camelCase attributes and uses them with all directives (*ngFor, ngModel, ...), and is also suggested by the Angular style guide.

Curator answered 4/1, 2016 at 10:25 Comment(16)
camelCase is not the preferred style for selectors of components. See Style 05-02Barbecue
That only applies to tag names. For attribute selectors there is no reason not to use camelCase. They would prefer camelCase names for tag selectors as well but this wouldn't match the WebComponents naming style where a - in the name is required. This doesn't cause much harm though because Angular doesn't depend on WebComponents features. The only minor difference is that Tags that don't have a - inherit from HTMLUnknownElement instead of HTMLElement See also html5rocks.com/en/tutorials/webcomponents/customelements/…Stomatal
It is suggested to avoid this according to Angular 2's style guide 05-03, angular.io/docs/ts/latest/guide/style-guide.html#!#05-03Revulsion
Sure, but there are situations where that is mandatory. How would you otherwise be able to make a <tr> or a <li> a component where no other tag names are supported?Stomatal
@LoganH But what's the alternative? According to w3c.github.io/webcomponents/spec/custom it seems to be valid HTML5 when a child-component gets rendered as <my-child-component> element. For modern browsers thats fine. If you have older ones, especially MS IE, this may cause trouble.Injunction
See Gunters comment right under mine, he explains one scenario where you can't avoid this. It is suggested to avoid, but if you have to do it, you canRevulsion
i wonder if angularjs's replace feature could be simulated with <ng-container myComponent>. i havent tried it yet.Maneater
It's not possible to set an attribute in a ng-container, i have tried it right now, but unfortunately it's not a solution @AlexanderTaylorHortenciahortensa
Not sure what you mean. <ng-container> is never added to the DOM, so adding attributes doesn't make senseStomatal
it's just not enough in some cases: #45937491Release
@GünterZöchbauer what about router-outlet component loading?Regurgitation
@BenRacicot I don't understand what you mean. <router-outlet> will add an element with the name of the components selector as sibling of <router-outlet>Stomatal
@GünterZöchbauer yes, can you load components without the wrapper selector there as well? With router-outlet we don't have the option of using attributes do we?Regurgitation
Can you please explain what you try to accomplish? There is no way to not have the wrapper element. What my answer above explains is, that the element tag can be custom. If you use the router-outlet, there is no way to specify a custom element name.Stomatal
@GünterZöchbauer I see. I'm trying to "flatten" my markup for more efficient use of CSS grid. It would be perfect for grid layout if we could display component view html without the wrapping element but doesnt look like that's possible... Thanks for replying.Regurgitation
@mik01aj Case sensitive support was only added several months after I posted this answer. Thanks a lot for reviving it :)Stomatal
D
11

To quote the Angular 1 to Angular 2 Upgrade Strategy doc:

Directives that replace their host element (replace: true directives in Angular 1) are not supported in Angular 2. In many cases these directives can be upgraded over to regular component directives.

In fact, it depends on what you want to do and you need to be aware that Angular2 supports several things:

According to what you want to do, you can choose different approaches. For your simple sample, it seems that the @Günter solution is the better ;-)

Datura answered 4/1, 2016 at 10:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.