更新数组对象值
Updating array object values
我想更新我保存在工厂中的全局数组中的一些值。
我使用 get 方法获取数据,但是 set 函数不知何故无法完成它的工作,并且数组中的值没有得到更新。我错过了什么?
.factory('messageList', function () {
var Messages =
[
{ "title":"Cash in", "icon":"ion-social-euro",
"dailyValue": "0", "weeklyValue": "0", "monthlyValue": "0",
"category": "financial", "active": "true"
},
{ "title":"Sales orders", "icon":"ion-social-euro",
"dailyValue": "0", "weeklyValue": "0", "monthlyValue": "0",
"category": "sales", "active": "true"
}
]
return {
get: function() {
return Messages;
},
set: function(title, key, newValue) {
for (var i = 0; i < Messages.length; i++) {
if(Messages[i].title == title){
Messages[i].key = newValue;
}
}
}
}
})
这是我尝试更新控制器中的值的方式:
messageList.set("Sales orders","dailyValue", $Scope.sum);
因为key
是一个变量,使用这个
Messages[i][key] = newValue;
Messages[i].key
将在您的对象中查找具有键 'key' 的元素,而不是具有变量值
的键
我想更新我保存在工厂中的全局数组中的一些值。 我使用 get 方法获取数据,但是 set 函数不知何故无法完成它的工作,并且数组中的值没有得到更新。我错过了什么?
.factory('messageList', function () {
var Messages =
[
{ "title":"Cash in", "icon":"ion-social-euro",
"dailyValue": "0", "weeklyValue": "0", "monthlyValue": "0",
"category": "financial", "active": "true"
},
{ "title":"Sales orders", "icon":"ion-social-euro",
"dailyValue": "0", "weeklyValue": "0", "monthlyValue": "0",
"category": "sales", "active": "true"
}
]
return {
get: function() {
return Messages;
},
set: function(title, key, newValue) {
for (var i = 0; i < Messages.length; i++) {
if(Messages[i].title == title){
Messages[i].key = newValue;
}
}
}
}
})
这是我尝试更新控制器中的值的方式:
messageList.set("Sales orders","dailyValue", $Scope.sum);
因为key
是一个变量,使用这个
Messages[i][key] = newValue;
Messages[i].key
将在您的对象中查找具有键 'key' 的元素,而不是具有变量值