@OlegBro

Передача данных между контроллерами?

Доброе утро!
Начал писать на Ангулар 5 и сразу же столкнулся с проблеммой передачи данных между незацязанными контроллерами. ПОмотрел в сети - ответы есть... но не работает почему то. Предоставляю код и буду рад совету. Заразнее спасибо.

Здесь контроллер который содержит лист репортов:

import { Component, OnInit } from '@angular/core';
import { Report } from '../models/report';
import { ReportsService } from './reports.service';
import { Message } from '@angular/compiler/src/i18n/i18n_ast';


@Component({
  selector: 'app-reports',
  templateUrl: './reports.component.html',
  styleUrls: ['./reports.component.css']
})
export class ReportsComponent implements OnInit {
  reports: Report[];
  selectedReport: Report;

  getReportData(reportId: number): void {
    this.reportService.getReport(reportId);
  }

  constructor(private reportService: ReportsService) { }

  ngOnInit() {
    this.getReportsList();
  }

}


Сервис
import { Injectable } from '@angular/core';
import { Observable, of } from 'rxjs';


import { Report } from '../models/report';
import { REPORTS } from '../reports-mock';

@Injectable({
  providedIn: 'root',
})
export class ReportsService {

  selectedReport: Observable<Report>;


  getReport(id: number): Observable<Report> {
    this.selectedReport =of (REPORTS.find(rep => rep.id === id));
    return this.selectedReport;
  }

  constructor() { }
}

и сам по себе "репорт" который получает переменную

import { Injectable } from '@angular/core';
import { Observable, of } from 'rxjs';


import { Report } from '../models/report';
import { REPORTS } from '../reports-mock';

@Injectable({
  providedIn: 'root',
})
export class ReportsService {

  selectedReport: Observable<Report>;

  getReportsList(): Observable<Report[]> {
    return of(REPORTS);
  }

  getReport(id: number): Observable<Report> {
    this.selectedReport =of (REPORTS.find(rep => rep.id === id));
    return this.selectedReport;
  }

  constructor() { }
}
  • Вопрос задан
  • 67 просмотров
Пригласить эксперта
Ответы на вопрос 1
byte916
@byte916
Попробуйте так

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


import { Report } from '../models/report';
import { REPORTS } from '../reports-mock';

@Injectable({
  providedIn: 'root',
})
export class ReportsService {

  selectedReport: Report;


  getReport(id: number): Report {
    this.selectedReport = REPORTS.find(rep => rep.id === id);
    return this.selectedReport;
  }

  constructor() { }
}


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


import { Report } from '../models/report';
import { REPORTS } from '../reports-mock';

@Injectable({
  providedIn: 'root',
})
export class ReportsService {

  selectedReport: Report;

  getReportsList(): Report[] {
    return REPORTS;
  }

  getReport(id: number): Report {
    this.selectedReport = REPORTS.find(rep => rep.id === id);
    return this.selectedReport;
  }

  constructor() { }
}
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы