Update name of an angular 2 component template

Is it possible to dynamically change the component template

<FAQ-omni></FAQ-omni>
based on a click event in the list?

<div class="row">
      <div class="col-xlg-4 col-xl-12 col-lg-12 col-md-7 col-sm-12 col-xs-12" title="FAQ" baCardClass="medium-card with-scroll">
        <FAQ-omni></FAQ-omni>
      <ul class="alShare">
        <li (click)="Show(1)">Locator</li>
        <li (click)="Show(2)">Offer</li>
        <li (click)="Show(3)">Contact</li>
        <li (click)="Show(4)">Holiday</li>
        <li (click)="Show(5)">FAQ</li>
      </ul>
      </div>
</div>

Answer №1

It is recommended not to alter the template selector as they are already specified on the page. Instead, consider creating a more generic template and utilizing @Input() to pass content in or interpolate it onto the page. Alternatively, you can pass in your predefined number.

<div class="row">
    <div class="col-xlg-4 col-xl-12 col-lg-12 col-md-7 col-sm-12 col-xs-12" title="FAQ" baCardClass="medium-card with-scroll">
        <FAQ [faqData]="faqData"></FAQ>
      <ul class="alShare">
        <li (click)="Show(1)">Locator</li>
        <li (click)="Show(2)">Offer</li>
        <li (click)="Show(3)">Contact</li>
        <li (click)="Show(4)">Holiday</li>
        <li (click)="Show(5)">FAQ</li>
      </ul>
    </div>
</div>

code-behind.ts

export class CodeBehind {

    public faqData: SomeObject = {//define default};

    public Show(faqToShow: number) {

        switch(faqToShow) {

        case 1: 
             this.faqData = Locator
        case 2:
        //and so on...

        }
    }
}

faq.component.ts

export class FAQ {

    @Input() public faqData: SomeObject;
}

If you follow the method outlined above, you can define the object in the parent component. However, passing a number allows you to define the content within FAQ separately from the parent. The choice is yours.

Answer №2

When it comes to component names, they are fixed in place. However, there is a way you can pass information to a component using the following approach:

Firstly, declare a variable within your component like so:

public selectedNumber: number;

Next, create a method within your component that will set the value of this variable:

public updateSelectedNumber(num: number)
{
    this.selectedNumber = num;
}

You can then use this selectedNumber property in your template to pass data like this:

<FAQ-selection selected="selectedNumber"></FAQ-selection>

Within your FAQ-selection component, you can receive this input variable as follows:

@Component({
    selector: 'FAQ-selection',
    templateUrl: '/path/to/template.html',
    inputs: ['selectedNumber']
})
export class LoginComponent
{
    public selectedNumber: number; 
}

Utilize this selectedNumber in your template to show or hide any content based on your requirements.

Additionally, you can leverage this variable to display different components based on certain conditions using the *ngIf directive as shown below:

<FAQ-selection *ngIf="selectedNumber == 1"></FAQ-selection>
<Another-Component *ngIf="selectedNumber == 2"></Another-Component>

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

Attempting to categorize JSON object elements into separate arrays dynamically depending on their values

Here's the JSON data I'm currently working with: ?$where=camis%20=%2230112340%22 I plan to dynamically generate queries using different datasets, so the information will vary. My main objective is to categorize elements within this array into ...

Modify the parent property's value within a derived Angular service

I'm utilizing Angular 9 and I have Service A along with Service B which extends Service A. Within Service A, there's a specific property that I aim to modify its value from within Service B. Surprisingly, the change only reflects in Service B and ...

Angular2 can enhance your webpage with a <div> element that covers the entire screen

While working on my Angular2 application, I encountered a challenging issue that I can't seem to resolve or find a solution for. Take a look at this image of my page: https://i.stack.imgur.com/b6KlV.png Code: content.component.html <app-header& ...

Avoid including any null or undefined values within a JSON object in order to successfully utilize the Object.keys function

My JSON data structure appears as follows: { 'total_count': 6, 'incomplete_results': false, 'items': [ { 'url': 'https://api.github.com/repos/Samhot/GenIHM/issues/2', 'repository_url' ...

Troubleshooting Vue 2 TypeScript Components Import Issue in VS Code

Has anyone experienced issues with TS pointing errors when importing custom components into a .vue file using the options api and webpack? The import is successful, everything works after bundling, but I'm still encountering annoying errors in the .vu ...

Optimizing Angular 2's Efficiency: A Deep Dive into Change Detection and Performance Metrics (1000 calls measured

As a newcomer to Angular 2, I recently inherited a project using the framework. Upon assessing the app's performance, I noticed it was running quite slowly and causing issues. I investigated further and discovered that many ngIf's and ngFor&apo ...

Troublesome bug in Angular 7: [ngModel] functionality fails to cooperate with mat-select

Having trouble implementing ngModel in the following scenario: Check out the template code below: <mat-select [ngModel]="data.dataObject[0].phase"> <mat-option *ngFor="let phase of possiblePhases" [value]=" ...

Access the Angular directive instance before it is actually rendered

Is it possible to access an Angular directive instance even if it has not been rendered yet? Let's say I have an app-component and a baz-directive. I want to be able to get the directive instance even if it is not currently shown or rendered in the D ...

What is the best way to integrate JSON:API with Angular and RxJs?

Currently, I have a Laravel API that adheres to the json:api spec and functions smoothly with Postman for making requests on related resources. However, I am facing challenges in understanding how to utilize this API with my Angular front-end. To kickstar ...

Disregarding TypeScript import errors within a monorepo ecosystem

In my Turborepo monorepo, I have a Next.js app package that imports various components from a shared package. This shared package is not compiled; it simply contains components imported directly by apps in the monorepo. The issue arises with the shared co ...

How to implement a custom pipe for dynamically changing image URLs in Ionic 3's image tag

I am trying to set authentication headers for images called from an image tag (<img>). To achieve this, I have created a custom pipe named secureimages using the command ionic g pipe secureimages. This pipe intercepts the HTTP requests in an interce ...

The type 'Observable<Student[]>' is lacking important properties such as id, first_name, last_name, English, and more within the type 'Student'

I have developed two components (table and form) and I am working on transferring data from the form component to the table component when editing. The code that I have written is currently throwing an error. However, when I check the console using console ...

Ways to ensure that DOM elements have completed rendering in Angular 2 Unit Tests

I am currently working on a test that focuses on an HTML element, sets its value, and triggers the blur event handler. The blur event is supposed to initiate a validation check which, if invalid, should disable a save button. During a test run, this proce ...

Converting Antdesign's Datepicker to Typescript

I'm having trouble figuring out how to properly annotate the dateObj parameter in the handleDateChange function that I've created. App.tsx import { useState } from 'react'; import logo from './logo.svg'; ...

Utilizing RxJS finalize in Angular to control the frequency of user clicks

Can someone provide guidance on using rxjs finalized in angular to prevent users from clicking the save button multiple times and sending multiple requests? When a button click triggers a call in our form, some users still tend to double-click, leading to ...

What is the process for changing the color of material-icons to white in an Angular Dart project using angular_components?

Is there a way to set the color of material-drawer material-icon to white on a dark background in an angular_components project using Angular Dart? app_component.html <material-drawer persistent #drawer="drawer" [class.custom-width]= ...

Module 'next-intl/client' cannot be located

When I run npm test, I encounter the following error: 'next-intl/client' module not found jest.mock( | ^ 22 | 'next-intl/client', 23 | (): Record<string, unknown> => ({ 24 | usePathname: ...

Dealing with a series of challenging npm problems consecutively

npm WARNING: Deprecated Usage Please note that global '--global' and '--local' configurations are now deprecated. Please use '--location=global' instead. Error Code: ERESOLVE Unable to resolve dependency tree. While reso ...

Enhance User Experience with Angular Material Autocomplete

I require assistance with implementing an auto-complete field using Angular Materials. Upon page load, a service is utilized to make an API call to a backend node application that communicates with a sandbox API to retrieve a list of supported cities. This ...

Issue with accessing storage in Ionic Storage (Angular)

Currently, I am attempting to utilize Ionic storage for the purpose of saving and loading an authentication token that is necessary for accessing the backend API in my application. However, I am encountering difficulties retrieving the value from storage. ...