Component A
I have a form with a button which should call a service class. The service returns an extracted JSON result.
compare() {
this.getResults();
}
getResults() {
this.resultService.getResults()
.subscribe(
results => this.results = results,
error => this.errorMessage = <any>error);
}
Template of A:
<form (ngSubmit)="compare()" #compareForm="ngForm">
<div class="col-md-5">
<h1>Disk Forensics</h1>
<div class="form-group">
<label for="resourceA">Disk A</label>
<select ngbDropDown class="form-control" id="resourceA" required [(ngModel)]="resourceA" name="resourceA">
<option *ngFor="let r of disks" [value]="r.id">{{r.name}}</option>
</select>
</div>
<div class="form-group">
<label for="resourceB">Disk B</label>
<select class="form-control" id="resourceB" required [(ngModel)]="resourceB" name="resourceB">
<option *ngFor="let r of disks" [value]="r.id">{{r.name}}</option>
</select>
</div>
<button type="button" (click)="compare()">Compare</button>
<div class="error" *ngIf="errorMessage">{{errorMessage}}</div>
</div>
<h4>Mode</h4>
<div class="radio">
<label>
<input type="radio" name="diffmode" id="diffmode1" value="diffmode1" checked>
different
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="diffmode" id="diffmode2" value="diffmode2">
identical
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="diffmode" id="diffmode3" value="diffmode3">
Left only
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="diffmode" id="diffmode4" value="diffmode4">
Right only
</label>
</div>
</form>
Service Class
public getResults(): Observable<Result[]> {
let body = JSON.stringify('{}');
var options:RequestOptionsArgs = {
body: "{}"
}
return this.results = this.http.get(this.url, options)
.map(this.extractData)
.catch(this.handleError);
}
Component B
Is just to display the results from the service
result.component.html:
<div class="col-md-12">
<h1>Results:</h1>
<div>
<p *ngFor="let r of results; let i=index">
{{i + 1}}: {{r}}
</p>
</div>
<div class="error" *ngIf="errorMessage">{{errorMessage}}</div>
</div>
result.component.ts:
import {Component, OnInit, Input} from '@angular/core';
import { Result } from "../models/result";
import {ResultService} from "../services/result.service";
@Component({
selector: 'diff-result',
templateUrl: './diff-result.component.html',
styleUrls: ['./diff-result.component.css'],
providers: [ResultService]
})
export class DiffResultComponent implements OnInit {
errorMessage: string;
constructor(private resultService: ResultService) { }
ngOnInit() {
this.getResults()
}
getResults() {
this.resultService.getResults()
.subscribe(
results => this.results = results,
error => this.errorMessage = <any>error);
}
}
What I'd like is to trigger from the compare()-function that the service is being called and the result.html is updated with the results.
Since this is a sibling component, assuming I can't use the ViewChild-attributes.
Many thanks for your help