Hi, I am on the Path of becoming Angular JS Developer. And have questions for module 1 ‘getting started with Angular’, ‘service’ section. And I have questions about the ‘OnInit’ and ‘ngOnInit’. What do they do, and how do we use them?
-
export class <class_name> {} usually export the class_name class and made it available for other components. Why do we implements OnInit here? What does it do?
-
ngOnInit. Some tutorial mentioned it is to initiate data for directive. How is it different from constructor? What data are we initiated ,what the data is used for? And when will they be initiated? Will it get initiated when we execute the ‘‘app-root’’ in HTML?
import { Component, OnInit } from ‘@angular/core’;
import { DemoService } from ‘./demo.service’;
@Component({
selector: ‘app-root’,
templateUrl: ‘./app.component.html’,
styleUrls: [’./app.component.css’]
})
export class AppComponent implements OnInit {
title = ‘angular-architecture’;
constructor(public demoService: DemoService) {}
ngOnInit(): void {
this.demoService.printHello();
}
}