How to create a data source

0 votes
With the help of code can you tell me How to create a data source?
Feb 24 in Angular by Nidhi
• 10,860 points
40 views

1 answer to this question.

0 votes

In Angular, a data source is commonly created using RxJS Observables or by extending DataSource from Angular Material for tables.

Using RxJS Observable as a Data Source

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

import { HttpClient } from '@angular/common/http';

import { Observable } from 'rxjs';

@Injectable({

  providedIn: 'root'

})

export class DataService {

  private apiUrl = 'https://api.example.com/data'; // Replace with actual API

  constructor(private http: HttpClient) {}

  getData(): Observable<any[]> {

    return this.http.get<any[]>(this.apiUrl);

  }

}

Usage in Component:

import { Component, OnInit } from '@angular/core';

import { DataService } from './data.service';

@Component({

  selector: 'app-data',

  template: `<ul><li *ngFor="let item of data">{{ item.name }}</li></ul>`

})

export class DataComponent implements OnInit {

  data: any[] = [];

  constructor(private dataService: DataService) {}

  ngOnInit() {

    this.dataService.getData().subscribe(response => this.data = response);

  }

}

answered Feb 24 by Navya

Related Questions In Angular

0 votes
1 answer

How to specify a port to run a create-react-app based project?

Hello @kartik, You could use cross-env to set the port, ...READ MORE

answered Jul 22, 2020 in Angular by Niroj
• 82,840 points
6,197 views
0 votes
1 answer

How to create a URL in the controller .NET MVC?

Hello @kartik, If you just want to get ...READ MORE

answered Jul 23, 2020 in Angular by Niroj
• 82,840 points
6,329 views
0 votes
1 answer

How to know tools and bundlers after create a new workspace or a project in angular?

Hello @sajal, When you create projects and workspaces ...READ MORE

answered Aug 6, 2020 in Angular by Niroj
• 82,840 points
1,138 views
0 votes
1 answer

How to pass data from a child component to a parent component in Angular 4?

In Angular 4, passing data from a ...READ MORE

answered Dec 4, 2024 in Angular by Navya
147 views
0 votes
1 answer

Should I use map or switchmap when using angular http module?

When working with the Angular HTTP module, ...READ MORE

answered Feb 24 in Angular by Navya
44 views
0 votes
1 answer

How does BehaviorSubject differ from Subject in state management?

Feature Subject BehaviorSubject Initial Value No initial value Requires an initial value Last ...READ MORE

answered Feb 24 in Node-js by Navya
38 views
0 votes
1 answer
0 votes
1 answer

What is the use of takeUntil to cancel a subscription?

takeUntil is an RxJS operator used to ...READ MORE

answered Feb 24 in Node-js by Navya
48 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP