вы можете использовать что-то вроде общего сервиса для передачи данных между двумя компонентами,
В этом сервисе мы могли бы использовать subject или eventEmitter (но тема лучше), а в компоненте домашней страницы мы можем выбросить эту тему
, находясь в компоненте карты поиска, мы подпишемся на эту тему для получения данных
. Вот пример
shared.service.ts
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
@Injectable({providedIn: 'root'}) // the service is available application wide, so the same instance of this class will be available in the whole application
export class SharedService {
searchSubject = new Subject<String>(); // a new subject of type String, replace it with your data type
}
и в домашнем компоненте, после выбора решения, просто отправьте его в общую службу, вы можете добавить (изменить) или (ngModelChange) в свой список в html, чтобы проверить изменения и один раз произошло изменение, выделите тему
, чтобы функция в домашнем компоненте html могла быть чем-то вроде
(ngModelChange) = "onSelectSolution()"
и в файле ts
constructor(private sharedService: SharedService) { // inject the shared service
}
onSelectSolution() {
// just emit the event
this.sharedService.searchSubject.next(this.selectedSolutionId); // emit the value to the shared service
}
затем в компоненте карты поиска
import { OnInit, OnDestroy } from '@angular/core';
import { Subscription } from 'rxjs';
// add the selector
export class SearchCardComponent implements OnInit, OnDestroy {
subscription: Subscription // define some subscription property to assign the subscription to, and destroy it once we left this component
constructor(private sharedService: SharedService){}
ngOnInit() {
// here to subscribe to the subject in the sharedService
this.subscription = this.sharedService.searchSubject.subscribe((mySearch: String) => {
// now we passed the search from the home component to the search card component
});
}
ngOnDestroy() {
// just unsubscribe the subscription we used
this.subscription.unsubscribe();
}
}
проверьте эту ссылку, чтобы увидеть разницу между (change)
и (ngModelChange)
разницей между (изменить) и (ngModelChange)