2

For some reason Angular 2 inserts these placeholder tags (for the lack of a better word) like router-outlet

In other words, for a basic app.component.ts like this:

import { Component } from '@angular/core';

@Component({
    selector   : 'app-root',
    templateUrl: './app.component.html',
    styleUrls  : ['./app.component.css']
})
export class AppComponent
{
}

this html output gets rendered in browser:

<router-outlet>
  Component rendered html content...
</router-outlet>

OR

<app-root>
  Component rendered html content...
</app-root>

instead of just having the component content inserted directly in html, like this:

<p>Component rendered html content...</p>

Edit


The existence of the router-outlet tag is preventing my css rules from being applied to (or even match) the component inner html tags

Is there a way I can change this behavior and get rid of the <app-route> (& other similar) tags?


NB. I'm new to Angular 2

TheDude
  • 3,045
  • 4
  • 46
  • 95
  • 1
    possible duplicate https://stackoverflow.com/a/43538081/681830 – Val Jun 02 '17 at 10:09
  • Format in your own css style for "app-root" and "router-outlet" like the rest of your

    . [link in StOvr](https://stackoverflow.com/questions/40017615/angular-2-styling-router-outlet-to-have-width-100)

    – Foo Bar Jun 02 '17 at 10:10
  • @Val same answer in same minute, with same opinion. can mark this as dup. – Foo Bar Jun 02 '17 at 10:11
  • @FooBar I'll do that if I have permission to =) – Val Jun 02 '17 at 10:12
  • @Val: not really, I'm asking to get rid of app-route, not how to apply styles. The underlying html/css templates used are way too complicated to start messing with them – TheDude Jun 02 '17 at 10:26
  • @FooBar: see my comment to Val – TheDude Jun 02 '17 at 10:27
  • @TheDude surely its much more difficult to change something that Angular does automatically, than to check the answer provided to re-style those tags. – Foo Bar Jun 02 '17 at 10:39
  • @FooBar: I actually *did* read the answer and went to angular website. see my comment to your answer – TheDude Jun 02 '17 at 10:50

1 Answers1

1

Into your styles.css file:

 :host /deep/ router-outlet{
//here you put your <p> css code for router-outlet
        display: block;
        border: 10px solid black;
    }
     :host /deep/ app-root{
//here you put your <p> css code for app-root
        display: block;
        border: 10px solid black;
    }

in case some rule you add you dont see it gets applied, add !important to the rule:

border: 10px solid black !important;

As an extra:

:host /deep/ router-outlet + *:not(nav) {
   width:80%;
   float:right;
}

the :not() selector excludes element in your template.

Foo Bar
  • 165
  • 2
  • 14
  • If I understood you correctly, you say I need to add `:host /deep/` to every single rule that applies to html elements inside all my components. Changing the existing html & css files would be a bad decision in this case, that means many inspecting many CSS files / hundreds of css rules to modify – TheDude Jun 02 '17 at 10:51
  • 1
    I know how to apply css, thank you. What I'm saying is that your proposed solution applies to simple cases. Unless I misread the angular documentation, I fail to see how adding a black border to `:host /deep/ router-outlet` via CSS would solve the gazillion issues with other CSS rules – TheDude Jun 02 '17 at 11:02
  • ...You? My solution applies to your question, stated problem was "this screws up styling" so thats what I tried to help you fix. But to remove router-outlet or hide it, theres [this post](https://stackoverflow.com/questions/40848062/angular-2-show-hide-router-outlet-and-redirect-to-html-page) – Foo Bar Jun 02 '17 at 11:07
  • Yes, I did get your intention when using borders, thank you. I edited my question, may be the issue is more clear now? – TheDude Jun 02 '17 at 11:13
  • can´t you simply copy those styles you want applied to "

    " (as you stated wanted result as:

    Component rendered html content...

    ) and also put them into the code I provided? That will mean to have 3 times same css, for

    , and , i think this is the easiest way to do it, and check if it generates "gazillions of issues" in css. Can´t help you further, sorry, can´t find how to get rid of them easily.

    – Foo Bar Jun 02 '17 at 11:20
  • I wish I could, I'm using metronic admin theme as the base for this webapp and there are way too many pages / styles rules to change :( – TheDude Jun 02 '17 at 11:23
  • 1
    Okay this is thinking out of the box, a little routine in javascript that changes from to

    , [you will have to modify the code but...](https://stackoverflow.com/questions/843680/how-to-replace-dom-element-in-place-using-javascript) This is not what should be done, but, maybe easier in your code than touching css, depends entirely on you.

    – Foo Bar Jun 02 '17 at 11:32