經過一些幫助之前與陣列我試圖檢索monthlyIncome
,savePercent
和年值並將它們存儲在$scope.monthlyIncome
,$scope.savePercent
和$scope.years
。
我哪裏錯了?
$scope.data2 = {
"$id":"4157e8b1-efa2-4feb-bf75-e907c0e200e0",
"$priority":null,
"date":1457178818625,
"email":"[email protected]",
"firstname":"test",
"lastname":"Isaacs",
"monthly":328947,
"monthlyIncome":1200,
"regUser":"4157e8b1-efa2-4feb-bf75-e907c0e200e0",
"savePercent":10,
"years":40
};
//for each object in data array
angular.forEach(data2, function(value, key) {
// check if value id is equals to 'monthlyIncome' OR 'savePercent' OR 'years'
if (value == 'monthlyIncome' ||
value == 'savePercent' ||
value == 'years') {
// add it's value with the same key (e.g. $scope.monthlyIncome)
$scope.[value] = key;
}
});
任何幫助,不勝感激:)
爲什麼?你已經有了使用'data2'對象的簡單訪問權限。對於那些'$ scope',沒有任何意義。你想解決什麼問題? – charlietfl
我同意@charlietfl,但是如果你堅持這樣做,只需從這個語句中刪除點:'$ scope。[value] = key;' – slackmart
你也混淆了對象的哪個部分是關鍵,哪個是值。學習使用'console.log()'或斷點將幫助你看到什麼不同的變量包含 – charlietfl