Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude code of app.component.html in my other components

app.component.html which is our default component has a menu added in it, but I don't wish to include it on my other component.

How can I do it?

The problem is when I am loading newsection.component.html, it is showing the app.component.html on top as shown below:

enter image description here

How can I restrict it so that it doesn't load above editsection.component.html?

like image 972
user2828442 Avatar asked Jun 30 '17 11:06

user2828442


People also ask

What is the use of APP component html?

The class AppComponent has a variable called title, which is displayed in the browser. It has just the html code and the variable title in curly brackets. It gets replaced with the value, which is present in the app. component.

What is AppComponent in Angular?

A bootstrapped component is an entry component that Angular loads into the DOM during the bootstrap process (application launch). Other entry components are loaded dynamically by other means, such as with the router. Angular loads a root AppComponent dynamically because it's listed by type in @NgModule.

What is a routed entry component?

Routed entryComponent: This sort of components are declared components and are added as an array inside the declarations section of the app. But, you may need to reference to a component by its class. Router components are not specified explicitly in the HTML of a component but, are registered in routes array.


2 Answers

You can do a combination of Dhyey and Hamed Baatour's answer.

First of all, create a separate component for your menu (e.g. menu.component.ts) and place all of your code in there.

Then modify your app.component.ts so that it looks like this:

<menu-component *ngIf="showMenu"></menu-component>
<router-outlet></router-outlet>

Important: Now you have to set showMenu in your app.component.ts and not in your newsection.component.ts.

You can do this by checking the requested URL. Just inject the router in your app.component.ts like this:

constructor(router:Router) {
router.events.forEach((event) => {
    if(event instanceof NavigationStart) {
        this.showMenu = event.url !== "/newsection";
    }
  });
}

This shouldn't show the menu if the route with /newsection is requested.

like image 180
Spitzbueb Avatar answered Nov 15 '22 21:11

Spitzbueb


First things first, you should understand the nature of Angular as it is a single page application which means a single HTML page is loaded and you can inject other code to it by using the router. But, as a workaround, you can use an *ngIf directive to show and hide your app component depending on where the user navigated in the app just like this:

if (this.router.url === '/login') {
this.showComponent = false
}

and in your template you do the following:

<app-root *ngIf="showComponent" ><app-root>
<router-outlet></router-outlet>

Edit

app-root is the selector of a new component that you create to move your app component HTML and logic into it. In your app.component.html template just include the HTML I provided in your app.component.ts inject the router and create the variable called showComponent and then in your ngOnInit or in your constructor add the if statement above to show or hide the app-root component depending on the router condition provided.

like image 36
Hamed Baatour Avatar answered Nov 15 '22 20:11

Hamed Baatour