Im trying to remove the called component tag from html to prevent a some broken css from external libraries (for example, calling a sidenav from clarity project):
something.component.ts
import {Component, OnInit, Input} from '@angular/core';
@Component({
selector: 'app-something',
templateUrl: './something.component.html',
styleUrls: ['./something.component.css']
})
export class SomethingComponent implements OnInit {
@Input() config: {} = {};
....
}
something.component.html
<div>
Hello World
</div>
another.component.html
<div>
<app-something [config]="somethingConfig"></app-something>
</div>
Then outputs:
<div>
<app-something>
<div>
Hello World
</div>
</app-something>
</div>
And I want:
<div>
<div>
Hello World
</div>
</div>
Yes, i know, many answers about it in stackoverflow and basically i can resume all the answers in 2 items :
selector: '[app-something]'
): Doesnt works, it triggers an error
The selector of the component SomethingComponent should be used as element (https://angular.io/styleguide#style-05-03) (component-selector).
templateUrl
param.Components are the most basic UI building block of an Angular app. An Angular app contains a tree of Angular components. Angular components are a subset of directives, always associated with a template. Unlike other directives, only one component can be instantiated for a given element in a template.
The ng-content is used when we want to insert the content dynamically inside the component that helps to increase component reusability. Using ng-content we can pass content inside the component selector and when angular parses that content that appears at the place of ng-content.
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.
From your component, change selector to div[app-something]
import {Component, OnInit, Input} from '@angular/core';
@Component({
selector: 'div[app-something]',
templateUrl: './something.component.html',
styleUrls: ['./something.component.css']
})
export class SomethingComponent implements OnInit {
@Input() config: {} = {};
....
}
Then you can use that component together with div
tag
<div>
<div app-something [config]="config"></div>
</div>
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