Angular 通过角度观察对象特性中的变化
在Angular2应用程序中,我有一个对象,其某些属性与此类似:Angular 通过角度观察对象特性中的变化,angular,rxjs,Angular,Rxjs,在Angular2应用程序中,我有一个对象,其某些属性与此类似: var person = { firstname:"Tom", lastname:"Hanks" }; 如果其中一个属性,例如“firstname”更改为“Bill”,我想在HTML页面中隐藏一些元素。 有没有办法观察这个物体的变化?使用RxJS的最佳方法是什么?我是Angular和RxJS的新手。你可以使用主题 export class YourService {
var person = {
firstname:"Tom",
lastname:"Hanks"
};
如果其中一个属性,例如“firstname”更改为“Bill”,我想在HTML页面中隐藏一些元素。
有没有办法观察这个物体的变化?使用RxJS的最佳方法是什么?我是Angular和RxJS的新手。你可以使用主题
export class YourService {
person$: Subject<Person> = new Subject<Person>();
setPerson(person) {
this.person$.emit(person);
};
}
你可以使用主题
export class YourService {
person$: Subject<Person> = new Subject<Person>();
setPerson(person) {
this.person$.emit(person);
};
}
const personSrc=新的行为主体({
名字:“汤姆”,
姓:“汉克斯”
};
const person$=this.personSrc.asObservable();
async
管道在模板中显示它
{{person.lastname}
一些性质
this.personSrc.next({firstname:“Bill”,lastname:“Watterson”})
const personSrc=新的行为主体({
名字:“汤姆”,
姓:“汉克斯”
};
const person$=this.personSrc.asObservable();
async
管道在模板中显示它
{{person.lastname}
一些性质
this.personSrc.next({firstname:“Bill”,lastname:“Watterson”})
您可以通过distinctUntilKeyChanged
distinctUntilKeyChanged
-仅当指定的键值
改变
例如:
console.clear();
import { BehaviorSubject } from "rxjs";
import { distinctUntilKeyChanged } from "rxjs/operators";
const personSrc = new BehaviorSubject<any>({
firstname: "Tom",
lastname: "Hanks"
});
setTimeout(() => {
const newPerson = { firstname: "Bill", lastname: "Smith" };
personSrc.next(newPerson);
}, 5000);
personSrc.pipe(distinctUntilKeyChanged("firstname")).subscribe(console.log);
console.clear();
从“rxjs”导入{BehaviorSubject};
从“rxjs/operators”导入{distinctUntilKeyChanged};
const personSrc=new BehaviorSubject您可以通过distinctUntilKeyChanged
distinctUntilKeyChanged
-仅当指定的键值
改变
例如:
console.clear();
import { BehaviorSubject } from "rxjs";
import { distinctUntilKeyChanged } from "rxjs/operators";
const personSrc = new BehaviorSubject<any>({
firstname: "Tom",
lastname: "Hanks"
});
setTimeout(() => {
const newPerson = { firstname: "Bill", lastname: "Smith" };
personSrc.next(newPerson);
}, 5000);
personSrc.pipe(distinctUntilKeyChanged("firstname")).subscribe(console.log);
console.clear();
从“rxjs”导入{BehaviorSubject};
从“rxjs/operators”导入{distinctUntilKeyChanged};
const personSrc=新行为主体