Changing Angular templates and styles dynamically based on different conditions

I have a header component structured as follows:

app-header (folder)
     classic (folder)
         app-header.component.html
         app-header.component.scss

    elegant (folder)
         app-header.component.html
         app-header.component.scss

    simple (folder)
         app-header.component.html
         app-header.component.scss

 app-header.component.ts

I am looking to dynamically switch between the template folders (classic, elegant, simple) based on data retrieved from a database using an HTTP request. I have already set the folder name in an environment variable and loaded the templates accordingly. However, I would like to learn how to set this environment variable by making an API call to further enhance the functionality.

Below is the code snippet I used to load the templates based on the environment variable in app-header-component.ts:

import { Component, OnInit } from '@angular/core';
import { ConfigService } from './../config.service';
import { environment } from './../../environments/environment';

let theme_name = environment.theme_name;

const header = require('./' + theme_name + '/app-header.component.html');

const style = [require('./' + theme_name + '/app-header.component.scss')];

@Component({
    selector: 'app-header',
    template: header,
    styles: style
})

Answer №1

The main component could manage the theme and determine which header component should be displayed.

<app-header-default *ngIf="theme === 'default'"></app-header-default>
<app-header-modern *ngIf="theme === 'modern'"></app-header-modern>
<app-header-basic *ngIf="theme === 'basic'"></app-header-basic>

Answer №2

Have you ever experimented with a similar approach?

@Component({
  selector: 'custom-selector',
  templateUrl: environment.template_path,
  styleUrls: [environment.style_path]
})

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Fixing Email Validation Error in AngularLearn how to troubleshoot and resolve

I'm encountering an issue when trying to develop an email center using regex pattern, as I'm receiving a validator error in HTML. I have already installed ngx-chips and angular-editor, imported all the necessary modules and dependencies. Here is ...

The problem encountered with the Enzyme mount API is a TypeError where it is unable to read the property 'find' of an undefined variable

After converting my component to a stateless one, I started encountering an error that says: TypeError: Cannot read property 'find' of undefined Previously, my tests were running smoothly before the switch. As far as I know, you can test functio ...

Toggle the visibility of a checkbox based on the JSON data

I have 4 check boxes that need to be dynamically displayed based on the value retrieved from my JSON. The JSON will only contain one checkbox name, and that specific checkbox should be shown to the user as checked. How can I achieve this functionality? Bel ...

What is the procedure for defining a variable within an ng-container?

Within another section of my Angular 12 web application, I currently utilize an ng-container with *ngFor to loop through an array and exhibit a control for each item in the array. This is how it's implemented: <ng-container *ngFor="let paramet ...

What causes branches coverage to appear haphazard?

I currently have 4 unit tests in place. const handleHttp = (method: 'POST' | 'GET' | 'PUT' | 'DELETE'): void => { const req = httpTestingController.expectOne(`${environment.apiUrl}test`); expect(req.request. ...

Position the dropdown beneath the button within the Chips Autocomplete component for proper alignment

I have enhanced the layout of the Chips Autocomplete component by placing a button above the dropdown, as illustrated in the image below. https://i.sstatic.net/d2ZD8.png My goal is to position the dropdown below the button, similar to this desired arrang ...

What are the differences between TypeScript's 'Dictionary' type accessors for objects and objects with predefined members?

Currently, I am delving into TypeScript by following an online tutorial. While my programming background primarily consists of 'structurally' typed languages like C and ActionScript 3, TypeScript presents some new concepts for me to grasp. One p ...

How can I change the date format from yyyymmdd to yyyy/mm/dd using an Angular pipe in HTML?

Can Angular convert a date in the yyyymmdd format to yyyy/mm/dd directly? I attempted using an Angular pipe, but it produced a different date result. The date I have is 20201029 {{date | date:'yyyy-MM-dd'}} ...

The switchMap function in Angular does not trigger the async validator as expected

To ensure that the username entered by the user is unique, I am sending an HTTP request for every input event from the target element. It is important to debounce this operation so that only one HTTP request is made for X consecutive input events within a ...

How can I ensure that PrimeNG is functioning properly?

I'm encountering some difficulties setting up PrimeNG correctly to utilize its rich text editor. Despite installing all the necessary components, it's still not functioning as expected. https://i.stack.imgur.com/4kdGf.png This is my package.jso ...

Angular is having trouble with disabled dates on the HTML5 Datepicker

I am encountering an issue with disabling past dates in a date-picker using HTML5. When I use the date-picker without any specific conditions, the disabled dates work as expected. However, when I try to use the date-picker with conditions, it does not fun ...

Tips for ensuring the Google Maps API script has loaded before executing a custom directive on the homepage of an Angular website

Issue - I am facing issues with Google Maps autocomplete drop-down not working on my website's main page even after parsing and loading the Google Maps API script. The problem seems to be a race condition on the main page of my website, specifically i ...

What is the process of invoking a service from a controller?

In my MovieSearchCtrl controller class, I have a method called returnMovies(query) that looks like this: returnMovies(query): any { return MovieSeat.MovieSearchService.getMovies(query); } Within the MovieSearchService service class, there is a functi ...

Leverage ngrx to streamline action creation for multiple feature modules

My Angular 5 application consists of multiple feature modules, each responsible for assets on specific pages and lazily loaded. src/ |--app/ |--core/ <-- core functionality here |--landing/ |--store/ |--about-us/ Each module has a top-leve ...

React and TypeScript threw an error: trying to execute setSearchQuery, which is not a function

I encountered an issue where I am receiving the error message: Uncaught TypeError: setSearchQuery is not a function in my Next.js / Typescript app. This error occurs while typing a search query into the search box. I have implemented a generic search funct ...

How can we send only the data from the first fieldset upon clicking a button in Angular reactive forms?

I am working on a project and need to send data from only the first fieldset when a button is clicked, not the second one. <form id="myform" [formGroup]="signupForm"> <fieldset> <mat-form-field class="example-full-width"> ...

Using Angular components that have @Input properties in a module separate from where they are originally defined

There is a component with an @Input declared below: @Component({ selector: 'app-api-error-message', templateUrl: './api-error-message.component.html', styleUrls: ['./api-error-message.component.scss'], inputs: ['E ...

Issue encountered while running the 'yarn install' command in a React project: 'The system does not recognize the term 'yarn'

When attempting to run the 'yarn install' command in my React project, I'm running into a problem. Even though I have Yarn installed globally (npm install --global yarn), I keep getting an error when trying to use any Yarn command in the ter ...

With each click on "add to cart," a duplicate of the component will appear

Can you help me achieve a functionality where every time I click on "addCart", a new instance of the same component (another Bets>Example Number</Bets> const newBet: React.FC = () => { const [getAddCart, setGetAddCart] = useState([ <Be ...

Managing the HttpErrorResponse within an Observable that does not have a boolean return value in the canActivate() method

My goal is to implement a canActivate() function that does the following: return false; this.router.navigateByUrl('/login'); This function should execute when the status code returned is 401 from an Observable, specifically when an HttpErrorRes ...