0

I have one component and one regular TypeScript class in an Angular project. The component extends the class. Both have an independent instance of ChangeDetectorRef. Something like this:

import { ChangeDetectorRef } from '@angular/core';

export class MySuperClass {
    private cdr: ChangeDetectorRef;
    constructor() {}
    // .. other code here
}

And in another file:

import { ChangeDetectorRef } from '@angular/core';

export class MyComponent extends {
    @Component({...})

    constructor(private cdr: ChangeDetectorRef) {
        super();
    }
    // .. other code here
}

What confuses me is that the instance of ChangeDetectorRef that is used form the super class works even though it is neither injected nor instantiated explicitly.

Is this behavior due to nature of ChangeDetectorRef or the nature of how inheritance works in TypeScript, or something completely else?

1 Answer 1

1

When angular injects

private cdr: ChangeDetectorRef

into MyComponent it actually runs over the the same param his father have.

If you create an instance of MySuperClass alone or if you change MySuperClass's cdr param name they both won't hold the same ChangeDetectorRef context

Sign up to request clarification or add additional context in comments.

2 Comments

So the parent class will get the child class instance of that variable even though I've explicitly declared two variables, both private?
The opposite. The parent instance holds the variable and because the child inherits from its parent, it uses the same variable. so the child gets the parent instance in some sort of way

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.