2016-03-05 73 views
-3

作为我的应用程序的一部分,我想提取的值为monthlyIncomesavePercent和年,并将它们保存为$scope。值,以便在用户登录时将它们设置为默认值!如何从内部具有密钥对的对象数组中拉取值?

[ 
    { 
     "$id": "date", 
     "$priority": null, 
     "$value": 1457178818625 
    }, 
    { 
     "$id": "email", 
     "$priority": null, 
     "$value": "[email protected]" 
    }, 
    { 
     "$id": "firstname", 
     "$priority": null, 
     "$value": "test" 
    }, 
    { 
     "$id": "lastname", 
     "$priority": null, 
     "$value": "Isaacs" 
    }, 
    { 
     "$id": "monthly", 
     "$priority": null, 
     "$value": 328947 
    }, 
    { 
     "$id": "monthlyIncome", 
     "$priority": null, 
     "$value": 123 
    }, 
    { 
     "$id": "percent", 
     "$priority": null, 
     "$value": 10 
    }, 
    { 
     "$id": "regUser", 
     "$priority": null, 
     "$value": "4157e8b1-efa2-4feb-bf75-e907c0e200e0" 
    }, 
    { 
     "$id": "savePercent", 
     "$priority": null, 
     "$value": 10 
    }, 
    { 
     "$id": "years", 
     "$priority": null, 
     "$value": 40 
    } 
] 

任何帮助将不胜感激。

+0

这不是可执行代码。 – wvdz

+0

我用完“旗帜”,请重新考虑您的问题。很难理解和理解你想要做什么。 –

+0

问题太模糊了。如果数据已经被传递到范围,那么显示的数据在哪里使用?为什么首先这不是一个对象? – charlietfl

回答

1

可以说你在$scope.data属性中存储你的数组。然后你需要检查这个数组中的每个对象。您可以使用angular.forEach函数来遍历数组并检查每个对象。

入住这plunker plnkr.co/edit/WeQhCb?p=preview 或以下代码:

//for each object in data array 
angular.forEach($scope.data, function(value, key) { 
    // check if value id is equals to 'monthlyIncome' OR 'savePercent' OR 'years' 
    if (value.$id == 'monthlyIncome' || 
     value.$id == 'savePercent' || 
     value.$id == 'years') { 
    // add it's value with the same key (e.g. $scope.monthlyIncome) 
    $scope[value.$id] = value.$value; 
    } 
}); 


现在很 $scope

monthlyIncome: {{monthlyIncome}} 
savePercent: {{savePercent}} 
years: {{years}} 


或者更优雅的方式来检查的属性是在阵列添加所需的道具并检查是否存在。完整代码:

(function(angular) { 
    'use strict'; 
    angular.module('app', []) 
    .controller('appController', function($scope, $log) { 
     $scope.data = [{ 
     "$id": "date", 
     "$priority": null, 
     "$value": 1457178818625 
     }, { 
     "$id": "email", 
     "$priority": null, 
     "$value": "[email protected]" 
     }, { 
     "$id": "firstname", 
     "$priority": null, 
     "$value": "test" 
     }, { 
     "$id": "lastname", 
     "$priority": null, 
     "$value": "Isaacs" 
     }, { 
     "$id": "monthly", 
     "$priority": null, 
     "$value": 328947 
     }, { 
     "$id": "monthlyIncome", 
     "$priority": null, 
     "$value": 123 
     }, { 
     "$id": "percent", 
     "$priority": null, 
     "$value": 10 
     }, { 
     "$id": "regUser", 
     "$priority": null, 
     "$value": "4157e8b1-efa2-4feb-bf75-e907c0e200e0" 
     }, { 
     "$id": "savePercent", 
     "$priority": null, 
     "$value": 10 
     }, { 
     "$id": "years", 
     "$priority": null, 
     "$value": 40 
     }]; 

     // required properties for $scope 
     var propertieIDs = ['monthlyIncome', 'savePercent', 'years']; 

     //for each object in data array 
     angular.forEach($scope.data, function(value, key) { 
     // check if value id is in 
     if (propertieIDs.indexOf(value.$id) !== -1) { 
      // add it's value with the same key (e.g. $scope.monthlyIncome) 
      $scope[value.$id] = value.$value; 
     } 
     }); 

    }); 
})(window.angular); 
+1

非常感谢!这正是我之后的:) – worc

相关问题