Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

212
Views
How to pass arguments and inject services to angular class?

A question seems fairly simple, but I don't work with OOP at all. I have a parent class. I want it to have service injected into constructor and to pass every argument needed in parent class. I just want to have an abstract class from which I can create more complex classes.

Let's say parent class looks like this:

export class FormComponent {
  constructor(childArgs: someType, private readonly SthService){
    this.sth = childArgs.sth;
    // etc
  }
}

where childArgs are arguments that I want to pass down to parent component and private readonly SthService is an injectable service I want to use in the component

The child class looks like that

@Component({
  selector: '...',
  templateUrl: './....html',
  styleUrls: ['./....scss']
})
export class ChildComponent extends FormComponent {
constructor(){
  super({
    firstProp: X,
    secondProp: Y,
    thirdProp: Z,
  });
}

What am I doing wrong? Can I just pass the arguments and have service injected altogether? Because right now it says that there SthService is undefined.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You are not providing correct parameters of base class FormComponent in derived class ChildComponent. So your constructor of ChildComponent should look like this:

export class ChildComponent extends FormComponent {
    constructor(childArgs: someType, private readonly SthService){
      super(childArgs, SthService)
    }
}

It is necessary to pass necessary arguments of constructor of base class in derived class.

So this is a code example of sending parameters to base class:

class Human {
    constructor(
        public name: string,
        public age: number
    ){}
}


class Employee extends Human {
    constructor(
        public name: string,
        public age: number,
        public dateHired: Date
    ) {
        super(name, age)
    }
}
about 4 years ago · Juan Pablo Isaza Report

0

use @inject('variable name') to pass other objects. I am assuming your service class name is ServiceClass.

    export class FormComponent {
        constructor(@Inject('childArgs') childArgs: any, private readonly SthService: ServiceClass){
          this.sth = childArgs.sth;
          // etc
        }
    }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!