Мое решение:
Создание службы
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs/Subject';
@Injectable()
export class ComponentInteractionService {
public name = new Subject<string>();
}
Компонент A:
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { ComponentInteractionService } from '../../services/component-interaction.service';
@Component({
selector: 'app-form-eins',
templateUrl: './form-eins.component.html',
styleUrls: ['./form-eins.component.css']
})
export class FormEinsComponent implements OnInit {
formEins: FormGroup;
constructor(private fb: FormBuilder,
private ci: ComponentInteractionService) { }
submit() {
this.ci.name.next(this.formEins.get("nameEins").value);
}
ngOnInit() {
this.formEins = this.fb.group({
nameEins: ''
});
this.ci.name.subscribe(
data => { this.formEins.get("nameEins").setValue(data); }
);
}
}
Компонент B:
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { ComponentInteractionService } from '../../services/component-interaction.service';
@Component({
selector: 'app-form-zwei',
templateUrl: './form-zwei.component.html',
styleUrls: ['./form-zwei.component.css']
})
export class FormZweiComponent implements OnInit {
formZwei: FormGroup;
constructor(private fb: FormBuilder,
private ci: ComponentInteractionService) { }
submit() {
this.ci.name.next(this.formZwei.get("nameEins").value);
}
ngOnInit() {
this.formZwei = this.fb.group({
nameEins: ''
});
this.ci.name.subscribe(
data => { this.formZwei.get("nameEins").setValue(data); }
);
}
}