Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 4 extend and implement

I have a class Hero which needs to extend GoogleCharts class. I also need to implement OnInit to get the some data from params.

How can I do this?

like image 490
Radu Pop Avatar asked Jun 14 '17 13:06

Radu Pop


People also ask

What is difference between extends and implements in Angular?

extends will get all properties and methods of the parent class. implements will obligate us to implement all of the properties and methods defined in the interface.

Can we use implements and extends together in TypeScript?

Yes you can do that.

What is implements OnInit in Angular?

A callback method that is invoked immediately after the default change detector has checked the directive's data-bound properties for the first time, and before any of the view or content children have been checked. It is invoked only once when the directive is instantiated.

What is extend in Angular?

Extends the destination object dst by copying own enumerable properties from the src object(s) to dst . You can specify multiple src objects. If you want to preserve original objects, you can do so by passing an empty object as the target: var object = angular. extend({}, object1, object2) .

Why is it important to implement components in angular?

It gives our applications a proper, maintainable and testable structure. At the core of every Angular application, we have a tree of components. Each component encapsulates the state (data), the HTML markup and the behavior for a view. At a minimum, a component is implemented using a class.

How do I implement a button on a page in angular?

A simple way to implement this is to create a button, call a method in the code which then uses the Angular router to navigate to the page. And what if you didn’t want to repeat the same code in each component? Typescript and Angular give you a way to handle this encapsulation. Inherited components!

How to implement di in angular?

Also, DI in our Angular components/services can be implemented using either constructor or injector. Let's try creating a basic service which we'll call as LoggingService. It would contain a method to log some predefined string and append whatever param is passed to it.

What is the use of services in angular?

Services are Angular classes that act as a central repository. They can be used to share common code across the app. Services are useful in making our component code much cleaner. Thus, components should mainly be responsible for the user interface rendering an user interaction - i.e. events and related things.


1 Answers

Just like this:

export class Hero extends GoogleCharts implements OnInit {...

If GoogleCharts already implements OnInit you should call super.ngOnInit(); before doing other stuff in your ngOnInit method.

Like this:

interface OnInit {
    ngOnInit: () => void;
}

class GoogleCharts implements OnInit{

    ngOnInit() {
        //does some stuff here
    }

}

class Hero extends GoogleCharts implements OnInit{

    ngOnInit() {
        super.ngOnInit();
        //do my stuff here
    }

}
like image 54
Spitzbueb Avatar answered Oct 05 '22 02:10

Spitzbueb