[英]Angular subscribe to object property changes
I have a service which holds some data: 我有一项服务,其中包含一些数据:
@Injectable()
export class DataService {
data = {
a: [
{label: 'label 1', checked: true},
{label: 'label 2', checked: true},
{label: 'label 3', checked: true}
]
}
}
and a component that imports the service and uses data from data.a
to generate checkboxes (with checked
property indicating the initial state of each checkbox). 一个组件,该组件导入服务并使用
data.a
数据来生成复选框(带有checked
属性指示每个复选框的初始状态)。 After user checks and unchecks boxes, values in DataService
are automatically updated by angular. 用户选中并取消选中复选框后,
DataService
中的值将自动按角度进行更新。
There is another service StorageService
which also injects DataService
and is supposed to store data.a
to browser's local storage automatically every time a checked
property is changed. 还有另一个服务
StorageService
,该服务还会注入DataService
,并应在每次更改checked
属性时自动将data.a
存储到浏览器的本地存储中。 What I can't figure out is how to observe those changes for individual object properties which are nested deeply like so? 我不知道如何观察像这样深深嵌套的单个对象属性的那些变化?
a
is some array of something? a
是有些东西阵? Array, Interface? 数组,接口?
In your service StorageService
you can access data througt 在服务
StorageService
您可以通过数据访问数据
constructor(private _dataService : DataService ){}
console.log(this._dataService.data.a[0].label);
Post more some code if this not working for you. 如果这不适合您,请发布更多代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.