【发布时间】:2016-04-15 12:23:37
【问题描述】:
我在 Angular JS 项目中这样的文件 (/static/models.json) 中有 json 数据..
{
"familyType": {
"A650": [{
"partID": "2630_H"
}, {
"partID": "2690_H"
}, {
"partID": "2680_H"
}, {
"partID": "2600_H"
}, {
"partID": "2620_H"
}, {
"partID": "2610_H"
}],
"S400": [{
"partID": "S500_Aru"
}, {
"partID": "S600_Aru"
}, {
"partID": "S700_Aru"
}, {
"partID": "S800_Aru"
}, {
"partID": "S900_Aru"
}, {
"partID": "S100_Aru"
}
]
}
}
我有下面的函数,参数传递到该方法(Part_id1,Part_id2)和函数内部..我有两种家庭类型(A650,S400),我需要检查是否两个输入 part_Id(Part_id1, Part_id2) 是否属于同一家族(即 A650 或 S400 )
如果两个 part_id(Part_id1, Part_id2) 来自不同的家庭类型,我需要提醒用户... 我们如何遍历 json 文件并比较两种家庭类型中的 part_ID ..
$scope.ValidateModelList = function(Part_id1, Part_id2, $http) {
if ($scope.firTable.selectedRowsCount != 0) {
$http.get("/static/models.json")
.success(function(response) {
$scope.names = response;
});
// here i am storing json data in $scope.names object
// here i need to check that the part_ids with the family
}
}
哪位大神能帮忙解决一下,不胜感激....
在此先感谢....
更新
function ValidateModel(partIDlist){
alert('2');
var data = stubdata; // Stubdata is actual json data mentioned above
var partIdResult = [];
for (var key in data.familyType) {
angular.forEach(data.familyType[key], function (object) {
angular.forEach(partIDlist, function (partId) {
if (partId == object.partID) {
partIdResult.push({
value: partId, family: key
});
}
});
});
}
for (var i = 0; i < partIdResult.length; i++) {
// alert(partIdResult.length);
//alert(partIdResult.family);
alert(JSON.stringify(partIdResult))
alert(partIdResult.family); // this is giving undefined value
alert(partIdResult[0].family);
if (partIdResult.family === partIdResult[0].family){
alert('yes');
alert("part_id's are same");
}
else if (partIdResult.family !== partIdResult[0].family) {
alert("part_id's not same");
}
}
}
我在这里传递一个这样的列表..
var partIDlist= ["S900_Aru", "S500_Aru", "2610_H"];
我得到 alert(JSON.stringify(partIdResult)) partIdResult 就像这样
[{"value":"S900_Aru","family":"S400"},
{"value":"S500_Aru","family":"S400"},
{"value":"2610_H","family":"A650"}]
我应该得到这三个 part_id 不属于同一个模型 .. 而不是这个我得到 part_id 属于同一个
你能帮忙吗...在此先感谢....
【问题讨论】:
标签: javascript jquery angularjs json angularjs-directive