I have a Maths class that allows me to alter the operation type.
export class Maths {
private static _instance: Maths = new Maths();
type: string = "ADD";
constructor() {
Maths._instance = this;
}
public static getInstance(): Maths {
console.log("Instance Provided");
return Maths._instance;
}
// update operation type method
changeType(newType) {
type = newType;
}
}
In one of my components, I have an instance of the Maths class and I need to detect changes in this instance, but it's not working. Below is the code snippet:
import { Component } from '@angular/core';
import { Maths } from './Maths';
@Component({
selector: 'operation-cmp',
template: ''
})
export class OperationComponent {
mathsType: Maths;
constructor() {
//Creating instance of Maths
this.mathsType = Maths.getInstance();
// Attempting to detect instance change
if(this.mathsType.type == "MULTIPLY") {
//Do something here
}
}
My app component updates the type of the Maths class on button click as shown below:
import { Component } from '@angular/core';
import { Maths } from './Maths';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
maths;
type = "MULTIPLY"
constructor() {
}
// This function is called on button click
changeType() {
this.maths = Maths.getInstance()
this.maths.changeType(type);
}
}
I am unsure how to detect changes in the Maths class's instance value in the OperationComponent. I need to perform certain actions based on the type within the OperationComponent.
Please assist me in finding a solution.