Angular 观察角度上的变量变化
我试图订阅angular service中变量的更改,而不需要在组件中订阅它。以下是我测试的代码: 服务:Angular 观察角度上的变量变化,angular,Angular,我试图订阅angular service中变量的更改,而不需要在组件中订阅它。以下是我测试的代码: 服务: import { Injectable } from '@angular/core'; import { Subject } from 'rxjs'; @Injectable() export class testService { detectChanges = new Subject(); myObject : any = { test:"Test" };
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
@Injectable()
export class testService {
detectChanges = new Subject();
myObject : any = {
test:"Test"
};
constructor() {
this.detectChanges.subscribe((data) => {
console.log("changed");
});
}
}
import { Component } from '@angular/core';
import { testService } from './test-service';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
$s;
constructor(private test: testService) {
this.$s = this.test.myObject;
}
}
<input [(ngModel)]="$s.test">
<h1>{{$s.test}}</h1>
private _myObject;
public get myObject() { return this._myObject; };
public set myObject(val) {
console.log("The value is changing");
this._myObject = val
};
constructor(public test: testService) {
this.test.myObject="New test"; // This would cause "The value is changing" to be logged
}
组件:
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
@Injectable()
export class testService {
detectChanges = new Subject();
myObject : any = {
test:"Test"
};
constructor() {
this.detectChanges.subscribe((data) => {
console.log("changed");
});
}
}
import { Component } from '@angular/core';
import { testService } from './test-service';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
$s;
constructor(private test: testService) {
this.$s = this.test.myObject;
}
}
<input [(ngModel)]="$s.test">
<h1>{{$s.test}}</h1>
private _myObject;
public get myObject() { return this._myObject; };
public set myObject(val) {
console.log("The value is changing");
this._myObject = val
};
constructor(public test: testService) {
this.test.myObject="New test"; // This would cause "The value is changing" to be logged
}
html:
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
@Injectable()
export class testService {
detectChanges = new Subject();
myObject : any = {
test:"Test"
};
constructor() {
this.detectChanges.subscribe((data) => {
console.log("changed");
});
}
}
import { Component } from '@angular/core';
import { testService } from './test-service';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
$s;
constructor(private test: testService) {
this.$s = this.test.myObject;
}
}
<input [(ngModel)]="$s.test">
<h1>{{$s.test}}</h1>
private _myObject;
public get myObject() { return this._myObject; };
public set myObject(val) {
console.log("The value is changing");
this._myObject = val
};
constructor(public test: testService) {
this.test.myObject="New test"; // This would cause "The value is changing" to be logged
}
{{$s.test}
有办法吗?就像angularjs$watch
这里有一个例子
在示例中,myObject将使用输入进行更改,您可以为此使用setter: 服务:
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
@Injectable()
export class testService {
detectChanges = new Subject();
myObject : any = {
test:"Test"
};
constructor() {
this.detectChanges.subscribe((data) => {
console.log("changed");
});
}
}
import { Component } from '@angular/core';
import { testService } from './test-service';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
$s;
constructor(private test: testService) {
this.$s = this.test.myObject;
}
}
<input [(ngModel)]="$s.test">
<h1>{{$s.test}}</h1>
private _myObject;
public get myObject() { return this._myObject; };
public set myObject(val) {
console.log("The value is changing");
this._myObject = val
};
constructor(public test: testService) {
this.test.myObject="New test"; // This would cause "The value is changing" to be logged
}
在组件中:
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
@Injectable()
export class testService {
detectChanges = new Subject();
myObject : any = {
test:"Test"
};
constructor() {
this.detectChanges.subscribe((data) => {
console.log("changed");
});
}
}
import { Component } from '@angular/core';
import { testService } from './test-service';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
$s;
constructor(private test: testService) {
this.$s = this.test.myObject;
}
}
<input [(ngModel)]="$s.test">
<h1>{{$s.test}}</h1>
private _myObject;
public get myObject() { return this._myObject; };
public set myObject(val) {
console.log("The value is changing");
this._myObject = val
};
constructor(public test: testService) {
this.test.myObject="New test"; // This would cause "The value is changing" to be logged
}
如果愿意,您也可以在set
功能中通知受试者
我在上创建了一个示例 基本上,您的服务如下所示:
@Injectable()
export class TestService{
private detectChanges: Subject<any> = new Subject<any>();
myobject$ = this.detectChanges.asObservable();
constructor() {
this.detectChanges.subscribe((data) => {
console.log("changed", data);
});
}
changeObject(myobject: any) {
this.detectChanges.next(myobject);
}
}
希望对您有所帮助。使用
行为主体
和getter/setter的最简单解决方案
@Injectable()
export class testService {
myObject: any = {
_test$: new BehaviorSubject("Test"),
set test(val) {
this._test$.next(val);
},
get test() {
return this._test$.getValue();
}
};
constructor() {
this.myObject._test$.subscribe((data) => {
console.log("changed", data);
});
}
}
另请查看。这仅在首次加载页面时有效。我想在myObject每次更改时检测更改。请查看更新。我添加了一个示例。在这个示例中,myObject将使用您正在查找的
Behavior Subject
(接收推送更新)输入而不是计划主题@Z.Bagley进行更改。我尝试过它,但它只在加载页面时第一次起作用。这很有效,但不是我需要的。我不想在组件中使用任何函数。请参见链接中的示例。@FerhadOthman,再看一看我的stackblitz,您需要使用getter/setter的混合来使其工作。