Obtain the selector from the parent element

Hello everyone,

I am currently working on developing a parent class called Event that will manage the DOM for four child classes: vote-event, view-event, my-events, and my-votes.

The concept is to have a single HTML file, a parent Component, and four child Components that will override certain methods of the parent. This scenario is a classic example of polymorphism.

Here is the parent component:

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

export abstract class EventsComponent implements OnInit{
    constructor() {};
    ngOnInit() {
        this.test();
    }
    abstract test();
}

This is the parent HTML structure:

<div id="main">
<div class="container pad-top">
  <div class="row">
    <div class="col-xs-12 nopadding text-center">
      <a [routerLink]="['/events/vote-events']"><img class="img-responsive sin-looks-votacion"
                                                     src="assets/images/no-tienes-votaciones.jpg"></a>
    </div>
  </div>
</div>

Now, in terms of implementation, I am aiming to achieve something like this:

Child Component:

export class MyVotesComponent extends EventsComponent {

    private potato;

    constructor() {
      super();
    }

    test() {
      console.log("Testttttttttttt");
    }
}

Child HTML:

<app-events></app-events>

However, I encountered an issue where I had to remove the EventsComponent from the events.module.ts declarations. As a result, the browser now throws an error:

'app-events' is not a known element:

So, my question is: Is this the most effective approach? If not, what would be a better alternative? What resources should I look into? And if this is indeed the right approach, what might I be missing?

Thank you all for your help,

Alejandro

Answer №1

To maintain the templateUrl and styleUrls, you will need to duplicate them in the @Component decorator of each subclass. However, organizing the files in one central location can help streamline this process:

@Component({
  selector: 'my-votes',
  templateUrl: './events.component.html',
  styleUrls: ['./events.component.css']
})
export class MyVotesComponent extends EventsComponent {

    private potato;

    constructor() {
      super();
    }

    test() {
      console.log("Testttttttttttt");
    }
}

Consider removing the @Component decorator from your abstract base class.

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

Incorporate a module that was developed locally into your project

Attempting to incorporate a locally developed Angular project/module into an angular application without having to publish it on the npm repository has been quite a challenge for me. To begin with, I decided to follow a tutorial that guided me through bui ...

Is my design going overboard with dependency injection while constructing loosely connected JavaScript modules?

I am in the process of developing a section of my website that will utilize a moderate amount of JavaScript, and I am striving to write it in the most efficient way possible. I am transitioning from a mindset rooted in JS 2010 to one more aligned with 2016 ...

When using Typescript type aliases, make sure to let Intellisense display the alias name instead of the source

Take a look at this brief code snippet type A = number; declare function f(): A; const a = f(); // `a` is number, not A What could be the reason for TS displaying a: number instead of a: A? ...

Manipulating Typescript JSON: Appending child attributes and showcasing them alongside the parent item attributes

Here is a JSON data that needs to be processed: var oldArr = [{ "careerLevel": "Associate", "careerLevels": [ { "201609": 21, "201610": 22, "careerID": "10000120" }, { "201609": 31, "201610": 32, ...

Is it possible to customize the color of the placeholder and clear-icon in the ion-search bar without affecting

I am working with two ion-search bars and I need to customize the placeholder and clear icon color for just one of them. <ion-searchbar class="search-bar" placeholder="search"></ion-searchbar> My goal is to target a specific i ...

Angular 4 Error: Jasmine Test Failed due to Uncaught Promise Rejection: Type Error - Unable to Access property 'nativeElement' when it is null

While working on unit test cases using Jasmine in my Angular 4 app, I keep encountering the error 'Cannot read property 'nativeElement' of null'. This issue is preventing me from checking if a button is clicked. Below, you can find the ...

Securely storing important information with mongodb/nodejs

I am currently working on a project that involves gathering sensitive information from users through my web application, such as a photo of their driver's license. I have set up a file upload feature using Angular to capture these images, and my initi ...

Sometimes the valueChanges of a Firestore collection in Take(1) can be inconsistent

Currently, I am incorporating AngularFire2 into my project. In the service that I have created, it looks like this: getItems(thing: string): Observable<Item[]> { return this.db.collections('item', ref => ref.where('things' ...

There was an issue encountered when trying to fill in the details for the Print Queue entity. Win32 error message: The name of the printer provided is

System.Printing.PrintQueueException: An error happened while populating the attributes for the PrintQueue object. Win32 error: The printer name is not valid. When I execute this code in Windows 10, I encounter this issue, although it works perfectly in Wi ...

What is the process for converting language json files into different languages?

I'm currently using ngx-translate to localize an Angular app. With over 20 languages that need translation, I am in search of a tool that can efficiently translate the language json files. While I did come across a helpful website, it proved to be ti ...

Angular 6 methods that handle Observable data from HTTP GET requests complete execution before constructing the final Data

My goal is to retrieve data from a REST endpoint and then construct a List of instances to return. However, I'm facing an issue where the HTTP GET function returns an Observable method that always results in an empty list before populating it with dat ...

How to highlight a specific date in angular 10's mat-date-range-input using a specific

Currently, I am working on a project that involves integrating the MatDatePicker component from Angular Material to create a customized calendar for specific dates. However, I have run into an issue when trying to add custom CSS classes to certain calendar ...

Update ngModel value following the PUT request response

I currently have a variable named dummy_value and I would like to update it using an input box. <p>{{dummy_value}}</p> <input [(ngModel)]="dummy_value" /> Upon making this change, the dummy_value updates instantly due to the two-way bin ...

Tips on creating a unit test for validating errors with checkboxes in your code base

In a certain scenario, I need to display an error message when a user clicks on the Next button without agreeing to the terms. To achieve this, I am looking to write a unit test case using Jest and React Testing Library. How can I go about doing this? im ...

Difficulty in transitioning observable information between inherited components

Having a base component and two inherited components, each component retrieves data from its own service which is stored in the state. When attempting to use the following approach, the data does not switch correctly: get codeData$(): Observable<Codes.C ...

Ideal method of re-entering ngZone following an EventEmitter event

There is a specific component that wraps around a library, and to prevent the chaos of change detection caused by event listeners in this library, the library is kept outside the Angular zone: @Component({ ... }) export class TestComponent { @Output() ...

Adding a custom role in Angular TypeScript in Microsoft AppInsights is a straightforward process that can provide valuable

I have an angular project where I am looking to incorporate AppInsight with custom telemetry (role). The project is built in Angular using TypeScript, and I successfully integrated appinsights by following this tutorial. However, when attempting to add cus ...

Troubleshooting: Data retrieval issues in AngularFire within Ionic and Angular Application

I am encountering errors and facing issues with data retrieval in my Angular and Ionic application when using AngularFire for Firebase integration. Here is a snippet from my package.json file: "dependencies": { "@angular/common": "5.0.3", "@angul ...

What could be causing me to receive a 404 error when trying to reload localhost in Angular?

Despite searching for solutions to my issue, I have not found any that have helped me. I have set up my routing in app-routing.module.ts: const routes: Routes = [ { path: 'home', component: LandingpageComponent, canActivate: [AuthGuard] }, ...

Just started a fresh Angular project, but the default page served by the 'ng serve' command isn't quite what

Last week, I delved into the world of Angular and kicked off a new project with ng new. After firing up the project with ng serve, I was greeted by a page that looked like this: ng serve default project page The app still functions properly, but the layou ...