angular services
Create a service using the command, ng g service <service name>.
service.ts:-
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class StudentdataService {
info1: string[]=["John Mathew",'E354',
'jm@abc.net']
constructor() { }
getInfo1():string[]{
return this.info1
}
}
in an y component:-
component.ts file
import { Component, OnInit }
from '@angular/core';
import { StudentdataService }
from '../services/studentdata.service';
@Component({
selector: 'app-aboutus',
templateUrl: './aboutus.component.html',
styleUrls: ['./aboutus.component.css'],
providers: [StudentdataService]
})
export class AboutusComponent
implements OnInit {
infoReceived1: string[]=[];
constructor(private data:StudentdataService) {
this.infoReceived1=this.data.getInfo1()
console.log(this.infoReceived1);
}
ngOnInit(): void {
}
}
component html file:
<ul>
<li *ngFor="let data of infoReceived1">
{{data}}
</li>
</ul>
Comments
Post a Comment