我正在尝试使用AngularJS(1.0)遍历多个对象的属性,并将每个对象的每个数据集作为无序列表返回。我使用直接的JQuery和JavaScript成功创建了此应用,但是不幸的是,它需要集成到一个相当大的AngularJS应用程序中。这就是我目前拥有的HTML,JSON和JavaScript。任何帮助将不胜感激:
JavaScript(mainScripts.js)
var mainApp = angular.module('mainApp',[]);
mainApp.controller('mainScripts', function($scope, $http) {
$http.get('devices.json').then(function successCallback(data){
console.log("API call works!");
// this callback will be called asynchronously
// when the response is available
console.log(data.deviceList);
//saying the above is undefined???
angular.forEach(data.deviceList, function(key, value){
$scope.titles = "<ul>" + "<li>" + key.location + "</li>"
+ "<li>" + key.description + "</li>"
+ "<li>" + key.instance + "</li>"
+ "<li>" + key.occupancy + "</li>"
+ "<li>" + key.schedule + "</li>"
+ "<li>" + key.deviceType[0] + "</li>" + "</ul>"
});
}, function errorCallback(data) {
console.log("API call doesn't work");
// called asynchronously if an error occurs
// or server returns response with an error status.
});
});
HTML
<html>
<head>
<meta charset="utf-8">
<title>Building Layout - Drag and Drop</title>
<link rel="stylesheet" type="text/css" href="main.css">
<script src="interact.js"></script>
<script src="angular.min.js"></script>
<script src="mainScripts.js"></script>
</head>
<body>
<div ng-app="mainApp" class="draggable deviceTiles" ng-controller="mainScripts">{{titles}}</div>
</body>
</html>
JSON(devices.json)
{
"deviceTypes": [{
"description": "Air Handler",
"typeName": "AirSource"
}, {
"description": "VAV Terminal",
"typeName": "AirTerminal"
}, {
"description": "Fan Coil",
"typeName": "ChilledWaterTerminal"
}, {
"description": "Chiller",
"typeName": "ChilledWaterSource"
}, {
"description": "Generic Unit",
"typeName": "NoResources"
}, {
"description": "Other Source",
"typeName": "OtherSource"
}, {
"description": "Other Terminal",
"typeName": "OtherTerminal"
}, {
"description": "Water Manager",
"typeName": "WaterSource"
}, {
"description": "WSHP Terminal",
"typeName": "WaterTerminal"
}],
"deviceList": [{
"href": "../MISRest/devices/3101117",
"location": "Loc Desk 3 VAV",
"description": "VAV 117",
"objectName": "VAV 117",
"instance": "3101117",
"occupancy": "Occupied",
"schedule": "Standard Schedule",
"ignore": "False",
"commStatus": "None",
"alarmStatus": "None",
"macaddress": "117",
"directSchedule": "True",
"rogueZone": "False",
"parentID": {
"air": "0"
},
"deviceType": ["AirTerminal"]
}, {
"href": "../MISRest/devices/3101121",
"location": "Loc Desk 4 with temp VAV",
"description": "VAV 121",
"objectName": "VAV Actuator Series 2 Bacnet ASC Controller",
"instance": "3101121",
"occupancy": "Error",
"schedule": "Standard Schedule",
"ignore": "False",
"commStatus": "Fault",
"alarmStatus": "Active",
"macaddress": "121",
"directSchedule": "True",
"rogueZone": "False",
"parentID": {
"air": "0"
},
"deviceType": ["AirTerminal"]
}, {
"href": "../MISRest/devices/3101004",
"location": "New Paris",
"description": "KMC Device",
"objectName": "BAC-8205_001635",
"instance": "3101004",
"occupancy": "Error",
"schedule": "Standard Schedule",
"ignore": "False",
"commStatus": "None",
"alarmStatus": "None",
"macaddress": "4",
"directSchedule": "True",
"rogueZone": "False",
"deviceType": ["NoResources"]
}, {
"href": "../MISRest/devices/3101013",
"location": "Default Location",
"description": "VAV-013",
"objectName": "DEFAULT",
"instance": "3101013",
"occupancy": "Occupied",
"schedule": "None",
"ignore": "False",
"commStatus": "None",
"alarmStatus": "None",
"macaddress": "13",
"directSchedule": "True",
"rogueZone": "False",
"parentID": {
"air": "0"
},
"deviceType": ["AirTerminal"]
}, {
"href": "../MISRest/devices/3101066",
"location": "Loc Desk AHU (1st)",
"description": "Desk AHU 066 (2nd)",
"objectName": "POL904_015413",
"instance": "3101066",
"occupancy": "Occupied",
"schedule": "None",
"ignore": "False",
"commStatus": "None",
"alarmStatus": "Active",
"macaddress": "66",
"directSchedule": "False",
"rogueZone": "False",
"deviceType": ["AirSource"]
}]
}
答案 0 :(得分:1)
首先,在successCallback中,您不能直接从回调响应中使用deviceList。响应在预定义的对象中返回,其中“数据”是该对象内部的属性之一。您需要按以下方式更改mainScripts.js。现在您不应该变得不确定。
mainApp.controller('mainScripts', function($scope, $http) {
$http.get('devices.json').then(function successCallback(response){
console.log("API call works!");
// this callback will be called asynchronously
// when the response is available
console.log(response.data.deviceList);
//saying the above is undefined???
angular.forEach(response.data.deviceList, function(key, value){
$scope.titles = "<ul>" + "<li>" + key.location + "</li>"
+ "<li>" + key.description + "</li>"
+ "<li>" + key.instance + "</li>"
+ "<li>" + key.occupancy + "</li>"
+ "<li>" + key.schedule + "</li>"
+ "<li>" + key.deviceType[0] + "</li>" + "</ul>"
});