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 {
  info1string[]=["John Mathew",'E354',
'jm@abc.net']
  constructor() { }
  getInfo1():string[]{

    return this.info1

  }
}



in an y component:-

component.ts file

import { ComponentOnInit } 
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 {
  infoReceived1string[]=[];
  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

Popular posts from this blog

interview questions js[ Anurag Singh ProCodrr]

reactnative_creation