example.service.ts
import { Injectable } from '@angular/core';
@Injectable()
export class ExampleService {
number1 : number;
number2 : number;
result : number;
constructor() { }
}
component1.component.ts
import { Component, OnInit } from '@angular/core';
import { ExampleService } from '../example.service'
@Component({
selector: 'app-component1',
templateUrl: './component1.component.html',
styleUrls: ['./component1.component.css'],
providers: [ExampleService]
})
export class Component1Component implements OnInit {
number1v = null;
number2v = null;
resultv = null;
constructor(public aService : ExampleService ) {
this.aService.number1 = this.number1v;
this.aService.number2 = this.number2v;
this.aService.result = this.resultv;
}
ngOnInit() {
}
processForm(){
this.resultv = this.number1v + this.number2v;
}
}
component2.component.ts
import { Component, OnInit } from '@angular/core';
import { ExampleService } from '../example.service'
@Component({
selector: 'app-component2',
templateUrl: './component2.component.html',
styleUrls: ['./component2.component.css'],
providers: [ExampleService]
})
export class Component2Component implements OnInit {
resultv;
constructor(public aService : ExampleService) {
this.aService.result = this.resultv;
}
ngOnInit() {
}
getResult(){
alert(this.resultv)
}
}
Want to pass a value of result from component1 to component2.
No parent to child or child to parent component.
the result i get is undefined.