Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度4-如何检测类实例值更新?_Angular_Typescript - Fatal编程技术网

Angular 角度4-如何检测类实例值更新?

Angular 角度4-如何检测类实例值更新?,angular,typescript,Angular,Typescript,我有一门数学课,在这门课上我可以改变操作的类型 export class Maths { private static _instance: Maths = new Maths(); type: string = "ADD"; constructor() { Maths._instance = this; } public static getInstance(): Maths { console.log("Instance Provided

我有一门数学课,在这门课上我可以改变操作的类型

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;
   }

   // change operation here
   changeType(newType) {
     type = newType;
   }
}
在我的一个组件中,我有一个数学类的实例,这里我需要检测这个实例中的变化。。但它不起作用。代码如下:

import { Component } from '@angular/core';
import { Maths } from './Maths';

@Component({
  selector: 'operation-cmp',
  template: ''
})

export class OperationComponent {
  mathsType: Maths;
  constructor() {

     //Maths instance
     this.mathsType = Maths.getInstance();

     // Trying to detect instance change
     if(this.mathsType.type == "MULTIPLY") {
        //Do something here
     }
}
我的应用程序组件设置此数学类的类型点击按钮,如下所示

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() {

 }

 // on button click calling this function
 changeType() {
  this.maths = Maths.getInstance()
  this.maths.changeType(type);
 }
}
我的疑问是,如何在OperationComponent中检测数学类的实例值变化。在操作中,组件需要根据类型执行一些功能

请帮助我找到解决方案。

您可以使用类型。在数学课上有一个主题实例,并在调用
changeType
时从中发出通知

export class Maths {
   private static _instance: Maths = new Maths();
   private typeChange: Subject = new Subject();
   type: string = "ADD";

   constructor() {
     Maths._instance = this;
   }

   public static getInstance(): Maths {
     console.log("Instance Provided");
     return Maths._instance;
   }

   public changeType(newType) {
     this.type = newType;
     this.typeChange.next(); // Put notification into container
   }

   public onTypeChange() : Subject {
      return this.typeChange;
   }
}
操作组件中
获取主题并订阅

export class OperationComponent implements OnInit {

  mathsType: Maths;

  constructor() {
     this.mathsType = Maths.getInstance();
  }

  ngOnInit() {
     this.mathsType.onTypeChange().subscribe(data => /*Your logic here*/)
  }

}
你可以用打字。在数学课上有一个主题实例,并在调用
changeType
时从中发出通知

export class Maths {
   private static _instance: Maths = new Maths();
   private typeChange: Subject = new Subject();
   type: string = "ADD";

   constructor() {
     Maths._instance = this;
   }

   public static getInstance(): Maths {
     console.log("Instance Provided");
     return Maths._instance;
   }

   public changeType(newType) {
     this.type = newType;
     this.typeChange.next(); // Put notification into container
   }

   public onTypeChange() : Subject {
      return this.typeChange;
   }
}
操作组件中
获取主题并订阅

export class OperationComponent implements OnInit {

  mathsType: Maths;

  constructor() {
     this.mathsType = Maths.getInstance();
  }

  ngOnInit() {
     this.mathsType.onTypeChange().subscribe(data => /*Your logic here*/)
  }

}

如果他们有亲子关系,那么就用a或者如果他们有亲子关系,那么就用a或者如果他们有亲子关系,那么就用a或者如果他们有亲子关系,那么就用a或者如果他们有亲子关系,那么就用a或者如果他们有亲子关系,那么就用a或者如果他们有亲子关系,那么用a或者如果他们有亲子关系,那么用a或者如果他们有亲子关系,那么就用,像一个reservation@RahulSingh:D.我已经完成了这套更好的西装a.我正在填写答案a.很好:D.这是我的第一次体验,就像reservation@RahulSingh我已经做完了