Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

202
Visualizações
How to access data in different components in Angular?

I am trying to build a student-teacher result view portal in which teachers can login and see all of the students result and add to that list or edit that list. Students can login to search using their credentials and see their own result.

I have a list of students (Array). I want to use that list in my student-view to search the list and return the student with the matching credentials. I want to use that list to provide the teachers with the CRUD functionality for complete list.

I could not figure out the correct way to get access to the list of students in different parts of my project.

I tried using @Input and Service but I couldnt do it in the correct way and I am getting empty array in both of the methods.

What is the correct way to acheive this? I have data in sibling component. Should I store data in parent component? Please help me find the correct way to do this.

This is the component where I have the data, students component. You can also see the component and project structure in this photo. Currently I am trying to transfer data between siblings using service and failed. I am setting the data in constructor using :

this.studentTransferData.setData(this.students)

this is where I have the data

I am trying to get data in my StudentView Component using :

  export class StudentViewComponent implements OnInit {

  name: string = ""
  rollno: number = 0
  
  studentList = this.studentDataTransferService.getData();

  @Input() students: Student[] = []
  @Output() studentSearch: EventEmitter<Student> = new EventEmitter();

  constructor(private studentDataTransferService: StudentDataTransferService) {
    console.log(this.students)
    console.log(this.studentList)
   }

It consoles empty array.

How to get the data in my different components. I have it in students component. Thanks.

Edit: I tried doing inside on ngInit as suggested in different post and it does not work as expected and I want it in complete project (siblings and parent to child) That is not what I want at the moment.

almost 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

When you need to share data between several components, you usually use a service.

To be truly reactive, you should also use what are called proxies : those are RxJS elements that can act both as observables and observers.

private _students = new BehaviorSubject<Student[]>([]);
public students$ = this._students.asObservable();

get students() { return this._students.value; }

loadStudents() {
  this.http.get<Student[]>(...).subscribe(students => this._students.next(students));
}

Then in other components

students$ = this.service.students$;
constructor(private service: StudentsService) {}
<ng-container *ngFor="let student of students$ | async">
  ...
</ng-container>

EDIT : using it in services

Yes, you can use it in services. If you need to make an HTTP call at some point, this is the best solution :


constructor(private service: StudentsService) {}

rewardStudent() {
  const bestStudent = this.service.students[0];
  this.http.post(...).subscribe();
}

Otherwise, you have access to students$ the same way you do with components.

almost 4 years ago · Santiago Trujillo Relatório

0

You can create and store the student list inside a student service class and also create a public function: getStudentsList inside your service and then call it from your different views.

almost 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda