Remove Host Component Tag from HTML in Angular 4

Remove host component tag from html in angular 4

you need to include tr as well in selector like below,

@Component({
selector: 'tr[my-component]',
template: `
<td>{{data1.prop1}}</td>
<td>{{data1.prop2}}</td>
<td>{{data2.prop1}}</td>
`
})
export class MyComponent {
@Input() data1;
@Input() data2;
}

Check this Plunker!!

Remove the host HTML element selectors created by angular component

Instead of trying to get rid of the host element, turn it into one that is valid SVG but other wise unaffecting: Instead of your element selector

selector: "svg-rect"

and its corresponding element in the template:

template: `...<svg-rect>...</svg-rect>...`

switch to an attribute selector:

selector: "[svg-rect]"

and add that attribute to a group element tag:

template: `...<g svg-rect>...</g>...`

This will expand to:

<svg height="550" width="450" x="0" y="0">
<g id="svgGroup">
<g svg-rect>
<!--template bindings={}-->
<rect x="10" y="10" height="100" width="100" fill="red" stroke="#000" stroke-width="2"></rect>
<!--template bindings={}-->
</g>
<g svg-rect>
<!--template bindings={}-->
<rect x="10" y="10" height="100" width="100" fill="red" stroke="#000" stroke-width="2"></rect>
<!--template bindings={}-->
</g>
</g>
</svg>

which is valid SVG, which will render.
Plnkr

Remove component tag in angular 6

Using the attribute selector seems to be the best way of solving this problem. The error you are getting is not due to angular not supporting the square brackets to achieve this, something else must be going wrong in your application. I suggest looking into examples on how to use the attribute selector for components.

Also remember to change

<div> 
<div>Hello World</div>
</div>

to

<div> 
<div app-something></div>
</div>

for the attribute selector to be effective.

Angular2 : render a component without its wrapping tag

You can use attribute selectors

@Component({
selector: '[myTd]'
...
})

and then use it like

<td myTd></td>

How to remove extra wrapping elements in the rendered HTML?

Angular components are directives with templates. According to this:

Directive configuration @Directive({ property1: value1, ... })

selector: '.cool-button:not(a)' Specifies a CSS selector that
identifies this directive within a template. Supported selectors
include element, [attribute], .class, and :not().

So component selectors can be also attribute selectors. For your example, instead of writing this:

parent.component.html:

<app-button>
<app-text>
My Text
</app-text>
</app-button>

write this:

parent.component.html:

<button app-button>
<span app-text>My Text</span>
</button>

where :

app-button.component.ts

...  
selector: '[app-button]',
template: `<ng-content></ng-content>
...

app-text.component.ts

...
selector: '[app-text]',
template: `<ng-content></ng-content>`
...

this would be rendered as you expected:

Sample Image

Update after your comment about styling those buttons:

To style the buttons from inside the button component, and set class in parent component, use :host-context pseudo-class. It is not deprecated and works well

button.component.css

  :host-context(.button-1)  {
background: red;
}
:host-context(.button-2) {
background: blue;
}

app.component.html

<button app-button class="button-1">
<span app-text>My Text</span>
</button>

<button app-button class="button-2">
<span app-text>My Text</span>
</button>

Here is the DEMO

Angular2: replace host element with component's template

Finally I found solution: injecting ElementRef to MyItem and using its nativeElement.innerHTML:

MyList:

import { Component, ContentChildren, QueryList } from 'angular2/core'
import { MyItem } from './myitem'

@Component({
selector: 'my-list',
template: `
<ul>
<li *ngFor="#item of items" [innerHTML]="item.innerHTML"></li>
</ul>
`
})
export class MyList {
@ContentChildren(MyItem) items: QueryList<MyItem>
}

MyItem:

import { Directive, Inject, ElementRef } from 'angular2/core'

@Directive({selector: 'my-item'})
export class MyItem {
constructor(@Inject(ElementRef) element: ElementRef) {
this.innerHTML = element.nativeElement.innerHTML
}
}

Working plunk is here



Related Topics



Leave a reply



Submit