Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove host component tag from html in angular 4

Tags:

I have a table in which I want to display a table row, which is a component. And also I want to pass data to that component:

<table>     <th>         <td>col 1</td>         <td>col 2</td>         <td>col 3</td>     </th>     <tr>         <my-component [data1]="data1" [data2]="data2"></my-component>     </tr>     <tr *ngFor="let item of list">         {{item}}     </tr> </table> 

In my-component, the HTML is a few <td></td> with data rendered from data1 and data2.

But after rendering it, because of <my-component></my-component> my CSS is breaking resulting in all my-component HTML (whole table row) displaying in 1st column.

Result of above:

<table>     <th>         <td>col 1</td>         <td>col 2</td>         <td>col 3</td>     </th>     <tr>         <my-component>             <td>data1.prop1</td>             <td>data1.prop2</td>         </my-component>     </tr>     <tr *ngFor="let item of list">         {{item}}     </tr> </table> 

I tried the following:

@Component({     selector: '[my-component]',     templateUrl: 'my-component.html' })  <tr my-component [data1]="data1" [data2]="data2"></tr> 

But this results in error Can't bind to 'data1' since it isn't a known property of 'tr'.

Also I can not use @Directive as I want to render HTML.

How can I render template of <my-component></my-component> without <my-component></my-component> tag?

Other answers are of previous versions of angular. I am using Angular 4.3.4.

Any help would be appreciated..!

like image 689
AjinkyaBhagwat Avatar asked Oct 10 '17 16:10

AjinkyaBhagwat


People also ask

How to remove HTML element from DOM using AngularJS?

How to Remove HTML element from DOM using AngularJS ? Here the task is to remove a particular element from the DOM with the help of AngularJS. Approach: Here first we select the element that we want to remove. Then we use remove () method to remove that particular element. Example 1: Here the element of class (‘p’) has been removed.

How does the host context selector work in angular?

So the :host-context selector takes another selector for instance the CSS class and it uses that to check whether the current element matches that selection, if so it applies the styles. Here is a full component example: Everything is set up in the styles without using any Angular specifics.

How do I disable an element in an expression in angular?

Set the disabled attribute to the result of an expression and Angular will disable / enable the element for you as needed. In example # 1, we have our component. There are two properties we are interested in: count and buttonDisabled. We will leverage these in our template. I ' ve been clicked {{count}} times. In example # 2, we have our template.

How to apply CSS Variable technique in angular component solution?

Now how to apply CSS variable technique into the Angular component solution. We need two things done. Firstly we need to change tile component so it uses the CSS variable as background color if provided. I will use the last code for tile component from :host section and implement usage of CSS variable:


1 Answers

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!!

like image 74
Madhu Ranjan Avatar answered Oct 18 '22 20:10

Madhu Ranjan