在我的 Angular JS 应用程序中,我globalData
使用服务从中央数据位置(恰当地称为 )调用数据:
数据服务.js:
angular.module('core').factory('dataService', function($http) {
let properties = {
globalData = {}
}
properties.subscribeTo = function(data) {
return this.globalData[data];
}
properties.loadData = function(data) {
// as per the component in the next section, data = 'myData'
// I've left out some code here but essentially the string 'myData'
// references an http url that is passed into the following $http.get
$http.get('...').then(res => this.globalData[data] = res);
}
return properties;
}
然后在一个组件中:
myComp.component.js:
angular.module('myComp').component('myComp', {
templateUrl: '...',
controller: function(dataService) {
this.$onInit = function() {
this.subscribedData = () => dataService.subscribeTo('myData');
if (!this.subscribedData()) dataService.loadData('myData');
}
}
});
myComp.template.html:
<div ng-repeat="data in $ctrl.subscribedData()">
{{...}}
</div>
这本身就可以正常工作(我提供此背景代码以防万一,因为可能有必要了解我来自哪里)。
现在说myData
我从dataService
(通过subscribeTo
方法)返回的看起来是这样的:
myData: [
{id: '1', personId: '1', date: '2018-05-06', firstName: 'John', lastName: 'Smith', datum: 'age', oldValue: '64', newValue: '65'},
{id: '2', personId: '1', date: '2018-05-06', firstName: 'John', lastName: 'Smith', datum: 'isRetired', oldValue: 'false', newValue: 'true'},
{id: '3', personId: '1', date: '2018-05-07', firstName: 'John', lastName: 'Smith', datum: 'job', oldValue: 'banker', newValue: ''},
{id: '4', personId: '2', date: '2018-05-08', firstName: 'Jane', lastName: 'Smith', datum: 'registeredSocialSecurity', oldValue: 'false', newValue: 'true'}
];
但我希望它看起来像这样:
myData = [
{id: '2', personId: '1' date: '2018-05-06', firstName: 'John', lastName: 'Smith', data: [{datum: 'age', oldValue: '64', newValue: '65'}, {datum: 'isRetired' ,oldValue: 'false', newValue: 'true'}]},
{id: '3', personId: '1', date: '2018-05-07', firstName: 'John', lastName: 'Smith', data: [{datum: 'isRetired', oldValue: 'false', newValue: 'true'}]},
{id: '4', personId: '2', date: '2018-05-08', firstName: 'Jane', lastName: 'Smith', data: [{datum: 'registeredSocialSecurity', oldValue: 'false', newValue: 'true'}]},
];
所以我将以下功能添加到我的component
:
myComp.component.js:
angular.module('myComp').component('myComp', {
templateUrl: '...',
controller: function(dataService) {
this.$onInit = function() {
this.subscribedData = () => dataService.subscribeTo('myData');
if (!this.subscribedData()) dataService.loadData('myData');
// New function here:
this.alteredData = function() {
let sub = (this.subscribedData() || []).reduce((obj, n) => {
let key = n.personId + '_' + n.date;
obj[key] = obj[key] || {};
obj[key].data = obj[key].data || {};
obj[key].id = n.id;
obj[key].personId = n.personId;
obj[key].date = n.date;
obj[key].firstName = n.firstName;
obj[key].lastName = n.lastName;
obj[key].data[n.datum] = {
datum: n.datum,
oldValue: n.oldValue,
newValue: n.newValue
};
return obj;
}, {});
for (let x in sub) {
sub[x].data = Object.values(sub[x].data);
}
return Object.values(sub);
}
}
}
});
但是,在尝试$ctrl.alteredData()
在模板上使用时给了我一个infdig
错误。
有任何想法吗?