我正在开发一个angularJS应用程序,我正在访问服务并获取如下的JSON数据。
{
"repositoryFileDto":[
{ "aclNode":"false",
"createdDate":"1433166794593",
"fileSize":"16208",
"folder":"false",
"hidden":"false",
"id":"d4850a7e-17f7-4ee6-a3c5-125c26077da8",
"lastModifiedDate":"1433166794593",
"locale":"en",
"localePropertiesMapEntries":[ ],
"locked":"false",
"name":"accountInfo.prpt",
"ownerType":"-1",
"path":"/home/admin/accountInfo.prpt",
"title":"accountInfo",
"versionId":"1.0",
"versioned":"true" },
{ },
{ },
{ }
]
}
从上面的数组中,我需要获取每个对象的name属性并将它们推送到List。
这是我的代码,但它说undefined不是函数。
$scope.filteredDashboards = [];
$scope.ExistingreportDetails = [];
$scope.GetReportDetails = function() {
$http({
method: 'GET',
url: 'http://192.168.1.201/api/repo/files/%3Ahome%3Aadmin/children?showHidden=false&filter=*',
// cache: $templateCache,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, X-Requested-With',
'Authorization': 'Basic YWRtaW46cGFzc3dvcmQ='
}
}).
success(function(data, status) {
$scope.ExistingreportDetails = angular.toJson(data);
$scope.filteredDashboards =$scope.ExistingreportDetails[0].repositoryFileDto ;
console.log($scope.ExistingreportDetails);
$scope.filteredDashboards.forEach(function(entry) {
console.log(entry);
});
$scope.ExistingreportDetails = $scope.ExistingreportDetails["1"].widgets;
//alert("success");
}).
error(function(data, status) {
alert("Request failed");
});
};
我需要像这样的oput,
$scope.reports =
[
{
name: "AODdetails",
title: "#f00"
},
{
name: "AccountInfo",
title: "#0f0"
}
]
答案 0 :(得分:1)
由于$scope.ExistingreportDetails
是一个对象,请尝试以下方法:
$scope.filteredDashboards = $scope.ExistingreportDetails.repositoryFileDto[0];
答案 1 :(得分:1)
看一下这个代码,点击按钮点击保存功能将被调用,值将存储在报表数组中。
<doctype html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
</head>
<body ng-app="myApp" data-ng-controller="HomeCtrl">
<button ng-click="save()">Save</button>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<script>
var app = angular.module('myApp',[]);
app.controller('HomeCtrl', function($scope){
$scope.data= {
"repositoryFileDto":[
{ "aclNode":"false",
"createdDate":"1433166794593",
"fileSize":"16208",
"folder":"false",
"hidden":"false",
"id":"d4850a7e-17f7-4ee6-a3c5-125c26077da8",
"lastModifiedDate":"1433166794593",
"locale":"en",
"localePropertiesMapEntries":[ ],
"locked":"false",
"name":"accountInfo.prpt",
"ownerType":"-1",
"path":"/home/admin/accountInfo.prpt",
"title":"accountInfo",
"versionId":"1.0",
"versioned":"true" },
{ "aclNode":"false",
"createdDate":"1433166794593",
"fileSize":"16208",
"folder":"false",
"hidden":"false",
"id":"d4850a7e-17f7-4ee6-a3c5-125c26077da8",
"lastModifiedDate":"1433166794593",
"locale":"en",
"localePropertiesMapEntries":[ ],
"locked":"false",
"name":"accountInfo1.prpt",
"ownerType":"-1",
"path":"/home/admin/accountInfo.prpt",
"title":"accountInfo",
"versionId":"1.0",
"versioned":"true"},
{ "aclNode":"false",
"createdDate":"1433166794593",
"fileSize":"16208",
"folder":"false",
"hidden":"false",
"id":"d4850a7e-17f7-4ee6-a3c5-125c26077da8",
"lastModifiedDate":"1433166794593",
"locale":"en",
"localePropertiesMapEntries":[ ],
"locked":"false",
"name":"accountInfo2.prpt",
"ownerType":"-1",
"path":"/home/admin/accountInfo.prpt",
"title":"accountInfo",
"versionId":"1.0",
"versioned":"true"},
{ "aclNode":"false",
"createdDate":"1433166794593",
"fileSize":"16208",
"folder":"false",
"hidden":"false",
"id":"d4850a7e-17f7-4ee6-a3c5-125c26077da8",
"lastModifiedDate":"1433166794593",
"locale":"en",
"localePropertiesMapEntries":[ ],
"locked":"false",
"name":"accountInfo3.prpt",
"ownerType":"-1",
"path":"/home/admin/accountInfo.prpt",
"title":"accountInfo",
"versionId":"1.0",
"versioned":"true" }
]
};
$scope.reports=[];
$scope.save=function()
{
for(var i=0; i<$scope.data.repositoryFileDto.length; i++)
{
var obj1={};
obj1.name=$scope.data.repositoryFileDto[i].name;
obj1.title=$scope.data.repositoryFileDto[i].title;
$scope.reports.push(obj1);
}
console.log($scope.reports);
};
});
</script>
</body>
</html>
答案 2 :(得分:0)
repositoryFileDto
是一个数组,所以:
$scope.filteredDashboards =$scope.ExistingreportDetails[0].repositoryFileDto[0] ;
编辑:
$scope.filteredDashboards = $scope.ExistingreportDetails.repositoryFileDto[0];
创建新阵列:
var newArray = $scope.filteredDashboards.map(function(el, id, arr){
return //What you want;
});
答案 3 :(得分:0)
你在哪一行得到错误?
您可以使用Angular的ForEach函数来推送对象或数组。 https://docs.angularjs.org/api/ng/function/angular.forEach
$buscar = $_POST['b'];
$buscarcarrera = $_POST['c'];
$whatIWant = substr($buscar, strpos($buscar, "Calle") + 5);
$whatIWant2 = substr($buscarcarrera, strpos($buscarcarrera, "Carrera") + 5);
$vacio = "Calle50A";
$vacioc = "Carrera50A";
if (preg_match('/[A-Za-z]/', $whatIWant))
{
buscar($buscar, "", $buscarcarrera, "");
}
else
{
buscar($buscar, $vacio, $buscarcarrera, $vacioc);
}
function buscar($b, $exclusion, $buscarcarrera, $exclusion2)
{
$con = mysql_connect('localhost', 'root', '');
mysql_select_db('map', $con);
$sql = mysql_query("SELECT * FROM finalmap WHERE calle LIKE '%" . $b . "%' AND calle not in ('$exclusion')", $con);
$contar = mysql_num_rows($sql);
if ($contar == 0)
{
echo "No se han encontrado resultados para '<b>" . $b . "</b>'.";
}
else
{
while ($row = mysql_fetch_array($sql))
{
$nombre = $row['calle'];
$id = $row['id'];
$lat = $row['latitud'];
$lon = $row['longitud'];
$dLat = '((latitud-'.$lat.')*PI()/180)';
$dLong = '((longitud-'.$lon.')*PI()/180)';
$a = '(sin('.$dLat.'/2) * sin('.$dLat.'/2) + cos('.$lat.'*pi()/180) * cos(map_coords_1*pi()/180) * sin('.$dLong.'/2) * sin('.$dLong.'/2))';
$sub_sql = '2*atan2(sqrt('.$a.'), sqrt(1-'.$a.'))';
$results = mysql_query(
"SELECT DISTINCT
id, calle, " . $sub_sql . " AS distance FROM finalmap
WHERE
calle LIKE '%" . $buscarcarrera . "%' AND calle not in ('$exclusion2')
ORDER BY
distance
", $con);
if ($results == 0)
{
echo "No se han encontrado resultados para '<b>" . $buscarcarrera . "</b>'.";
}
else
{
while ($row2 = mysql_fetch_array($results)) {
echo "Calle: " . $row2['calle'] . " Miles - " . $row2['distance']*3956;
echo "Calle: " . $row2['calle'] . " Kilometers - " . $row2['distance']*6367;
echo "Calle: " . $row2['calle'] . " Nautical Miles - " . $row2['distance']*3435;
}
}
}
}
}
您已经从HTTP请求中返回了一个json对象,因此在这里转换为json对象毫无意义:
angular.forEach(data.repositoryFileDto[0], function(value) {
this.push(value);
}, $scope.ExistingreportDetails);
因此,如果您想输出“aclNode”的值,您只需执行
即可$scope.ExistingreportDetails = angular.toJson(data);
或者,如果您将数据结果分配给$ scope.ExistingreportDetails:
console.log(data.repositoryFileDto[0].aclNode);
答案 4 :(得分:0)
我想你需要以这种方式使用angular.forEach()
方法:
试试这个:
success(function(data, status) {
$scope.ExistingreportDetails = angular.toJson(data);
console.log($scope.ExistingreportDetails);
angular.forEach($scope.ExistingreportDetails, function(entry, i) {
$scope.filteredDashboards.push(entry.name); // push the name from the object
});
$scope.ExistingreportDetails = $scope.ExistingreportDetails["1"].widgets;
//alert("success");
})