Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove component tag in angular 6

Tags:

angular

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 :

  • Add brackets to selector (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).

  • Use a Directive: Directives cannot have templateUrl param.
like image 731
Stefan Luv Avatar asked Jun 05 '18 07:06

Stefan Luv


People also ask

What is component in angular?

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.

What does ng content do?

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.


Video Answer


2 Answers

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.

like image 160
enf0rcer Avatar answered Sep 19 '22 09:09

enf0rcer


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> 
like image 29
Tran Son Hoang Avatar answered Sep 21 '22 09:09

Tran Son Hoang