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..!
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.
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.
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.
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:
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!!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With