我正在努力让$resource.query()
工作。我已经设置了一个简单的服务来获取我存储在一个文件夹中的.json项目列表。虽然我认为我使用了正确的代码,但我仍然收到以下错误:
错误:[$ resource:badcfg]操作的资源配置出错
query
。预期的响应包含一个数组但得到一个对象 (请求:GET /数据/仪表板)
如何使用单独的文件?
P.S。我正在使用nodeJS和http-server创建localhost:8000环境来查看我的操作结果。
我的文件夹结构:
/..
/data
/dashboard //contains 1 .json file for every item I would like to monitor in my dashboard
1.json
2.json
3.json
/js
/controllers
dashboardCtrl.js
/directives
/services
dashboardResource.js
myDash.js
/lib //(This folder contains the AngularJS files)
myDash.js
var myDash = angular.module("myDash", ["ngRoute", "ngResource"]);
myDash.config(function($routeProvider, $locationProvider) {
$routeProvider.
when("/dashboard", {
templateUrl: "templates/dashboard.html",
controller: "dashboardCtrl",
}).
otherwise({
redirectTo: "/dashboard"
});
$locationProvider.html5Mode(false);
});
dashboardCtrl.js
myDash.controller("dashboardCtrl", function($scope, $routeParams, dashboardResource, $location){
console.log(dashboardResource.query());
$scope.items = dashboardResource.query();
});
dashboardResource.js
myDash.factory("dashboardResource", function ($resource, $routeParams) {
return $resource("/data/dashboard/:id", {id: "@id"}, {
query: {method: "GET", isArray: false}
});
});
响应(/ data / dashboard /索引的HTML代码)
e {0: "<", 1: "!", 2: "d", 3: "o", 4: "c", 5: "t", 6: "y", 7: "p", 8: "e", 9: " ", 10: "h", 11: "t", 12: "m", 13: "l", 14: ">", 15: "↵", 16: "<", 17: "h", 18: "t", 19: "m", 20: "l", 21: ">", 22: "↵", 23: " ", 24: " ", 25: "<", 26: "h", 27: "e", 28: "a", 29: "d", 30: ">", 31: "↵", 32: " ", 33: " ", 34: " ", 35: " ", 36: "<", 37: "m", 38: "e", 39: "t", 40: "a", 41: " ", 42: "c", 43: "h", 44: "a", 45: "r", 46: "s", 47: "e", 48: "t", 49: "=", 50: """, 51: "u", 52: "t", 53: "f", 54: "-", 55: "8", 56: """, 57: ">", 58: "↵", 59: " ", 60: " ", 61: " ", 62: " ", 63: "<", 64: "m", 65: "e", 66: "t", 67: "a", 68: " ", 69: "n", 70: "a", 71: "m", 72: "e", 73: "=", 74: """, 75: "v", 76: "i", 77: "e", 78: "w", 79: "p", 80: "o", 81: "r", 82: "t", 83: """, 84: " ", 85: "c", 86: "o", 87: "n", 88: "t", 89: "e", 90: "n", 91: "t", 92: "=", 93: """, 94: "w", 95: "i", 96: "d", 97: "t", 98: "h", 99: "="…}0: "<"1: "!"2: "d"3: "o"4: "c"5: "t"6: "y"7: "p"8: "e"9: " "10: "h"11: "t"12: "m"13: "l"14: ">"15: "↵"16: "<"17: "h"18: "t"19: "m"20: "l"21: ">"22: "↵"23: " "24: " "25: "<"26: "h"27: "e"28: "a"29: "d"30: ">"31: "↵"32: " "33: " "34: " "35: " "36: "<"37: "m"38: "e"39: "t"40: "a"41: " "42: "c"43: "h"44: "a"45: "r"46: "s"47: "e"48: "t"49: "="50: """51: "u"52: "t"53: "f"54: "-"55: "8"56: """57: ">"58: "↵"59: " "60: " "61: " "62: " "63: "<"64: "m"65: "e"66: "t"67: "a"68: " "69: "n"70: "a"71: "m"72: "e"73: "="74: """75: "v"76: "i"77: "e"78: "w"79: "p"80: "o"81: "r"82: "t"83: """84: " "85: "c"86: "o"87: "n"88: "t"89: "e"90: "n"91: "t"92: "="93: """94: "w"95: "i"96: "d"97: "t"98: "h"99: "="100: "d"101: "e"102: "v"103: "i"104: "c"105: "e"106: "-"107: "w"108: "i"109: "d"110: "t"111: "h"112: """113: ">"114: "↵"115: " "116: " "117: " "118: " "119: "<"120: "t"121: "i"122: "t"123: "l"124: "e"125: ">"126: "I"127: "n"128: "d"129: "e"130: "x"131: " "132: "o"133: "f"134: " "135: "/"136: "d"137: "a"138: "t"139: "a"140: "/"141: "d"142: "a"143: "s"144: "h"145: "b"146: "o"147: "a"148: "r"149: "d"150: "/"151: "<"152: "/"153: "t"154: "i"155: "t"156: "l"157: "e"158: ">"159: "↵"160: " "161: " "162: "<"163: "/"164: "h"165: "e"166: "a"167: "d"168: ">"169: "↵"170: " "171: " "172: "<"173: "b"174: "o"175: "d"176: "y"177: ">"178: "↵"179: "<"180: "h"181: "1"182: ">"183: "I"184: "n"185: "d"186: "e"187: "x"188: " "189: "o"190: "f"191: " "192: "/"193: "d"194: "a"195: "t"196: "a"197: "/"198: "d"199: "a"200: "s"201: "h"202: "b"203: "o"204: "a"205: "r"206: "d"207: "/"208: "<"209: "/"210: "h"211: "1"212: ">"213: "↵"214: "<"215: "t"216: "a"217: "b"218: "l"219: "e"220: ">"221: "<"222: "t"223: "r"224: ">"225: "<"226: "t"227: "d"228: ">"229: "<"230: "c"231: "o"232: "d"233: "e"234: ">"235: "("236: "d"237: "r"238: "w"239: "-"240: "r"241: "w"242: "-"243: "r"244: "w"245: "-"246: ")"247: "<"248: "/"249: "c"250: "o"251: "d"252: "e"253: ">"254: "<"255: "/"256: "t"257: "d"258: ">"259: "<"260: "t"261: "d"262: " "263: "s"264: "t"265: "y"266: "l"267: "e"268: "="269: """270: "t"271: "e"272: "x"273: "t"274: "-"275: "a"276: "l"277: "i"278: "g"279: "n"280: ":"281: " "282: "r"283: "i"284: "g"285: "h"286: "t"287: ";"288: " "289: "p"290: "a"291: "d"292: "d"293: "i"294: "n"295: "g"296: "-"297: "l"298: "e"299: "f"300: "t"301: ":"302: " "303: "1"304: "e"305: "m"306: """307: ">"308: "<"309: "c"310: "o"311: "d"312: "e"313: ">"314: "<"315: "/"316: "c"317: "o"318: "d"319: "e"320: ">"321: "<"322: "/"323: "t"324: "d"325: ">"326: "<"327: "t"328: "d"329: " "330: "s"331: "t"332: "y"333: "l"334: "e"335: "="336: """337: "p"338: "a"339: "d"340: "d"341: "i"342: "n"343: "g"344: "-"345: "l"346: "e"347: "f"348: "t"349: ":"350: " "351: "1"352: "e"353: "m"354: """355: ">"356: "<"357: "a"358: " "359: "h"360: "r"361: "e"362: "f"363: "="364: """365: "/"366: "d"367: "a"368: "t"369: "a"370: "/"371: "d"372: "a"373: "s"374: "h"375: "b"376: "o"377: "a"378: "r"379: "d"380: "/"381: "."382: "."383: "/"384: """385: ">"386: "."387: "."388: "/"389: "<"390: "/"391: "a"392: ">"393: "<"394: "/"395: "t"396: "d"397: ">"398: "<"399: "/"400: "t"401: "r"402: ">"403: "↵"404: "<"405: "t"406: "r"407: ">"408: "<"409: "t"410: "d"411: ">"412: "<"413: "c"414: "o"415: "d"416: "e"417: ">"418: "("419: "-"420: "r"421: "w"422: "-"423: "r"424: "w"425: "-"426: "r"427: "w"428: "-"429: ")"430: "<"431: "/"432: "c"433: "o"434: "d"435: "e"436: ">"437: "<"438: "/"439: "t"440: "d"441: ">"442: "<"443: "t"444: "d"445: " "446: "s"447: "t"448: "y"449: "l"450: "e"451: "="452: """453: "t"454: "e"455: "x"456: "t"457: "-"458: "a"459: "l"460: "i"461: "g"462: "n"463: ":"464: " "465: "r"466: "i"467: "g"468: "h"469: "t"470: ";"471: " "472: "p"473: "a"474: "d"475: "d"476: "i"477: "n"478: "g"479: "-"480: "l"481: "e"482: "f"483: "t"484: ":"485: " "486: "1"487: "e"488: "m"489: """490: ">"491: "<"492: "c"493: "o"494: "d"495: "e"496: ">"497: "3"498: "9"499: "3"500: "B"501: "<"502: "/"503: "c"504: "o"505: "d"506: "e"507: ">"508: "<"509: "/"510: "t"511: "d"512: ">"513: "<"514: "t"515: "d"516: " "517: "s"518: "t"519: "y"520: "l"521: "e"522: "="523: """524: "p"525: "a"526: "d"527: "d"528: "i"529: "n"530: "g"531: "-"532: "l"533: "e"534: "f"535: "t"536: ":"537: " "538: "1"539: "e"540: "m"541: """542: ">"543: "<"544: "a"545: " "546: "h"547: "r"548: "e"549: "f"550: "="551: """552: "/"553: "d"554: "a"555: "t"556: "a"557: "/"558: "d"559: "a"560: "s"561: "h"562: "b"563: "o"564: "a"565: "r"566: "d"567: "/"568: "1"569: "."570: "j"571: "s"572: "o"573: "n"574: """575: ">"576: "1"577: "."578: "j"579: "s"580: "o"581: "n"582: "<"583: "/"584: "a"585: ">"586: "<"587: "/"588: "t"589: "d"590: ">"591: "<"592: "/"593: "t"594: "r"595: ">"596: "↵"597: "<"598: "t"599: "r"600: ">"601: "<"602: "t"603: "d"604: ">"605: "<"606: "c"607: "o"608: "d"609: "e"610: ">"611: "("612: "-"613: "r"614: "w"615: "-"616: "r"617: "w"618: "-"619: "r"620: "w"621: "-"622: ")"623: "<"624: "/"625: "c"626: "o"627: "d"628: "e"629: ">"630: "<"631: "/"632: "t"633: "d"634: ">"635: "<"636: "t"637: "d"638: " "639: "s"640: "t"641: "y"642: "l"643: "e"644: "="645: """646: "t"647: "e"648: "x"649: "t"650: "-"651: "a"652: "l"653: "i"654: "g"655: "n"656: ":"657: " "658: "r"659: "i"660: "g"661: "h"662: "t"663: ";"664: " "665: "p"666: "a"667: "d"668: "d"669: "i"670: "n"671: "g"672: "-"673: "l"674: "e"675: "f"676: "t"677: ":"678: " "679: "1"680: "e"681: "m"682: """683: ">"684: "<"685: "c"686: "o"687: "d"688: "e"689: ">"690: "3"691: "9"692: "4"693: "B"694: "<"695: "/"696: "c"697: "o"698: "d"699: "e"700: ">"701: "<"702: "/"703: "t"704: "d"705: ">"706: "<"707: "t"708: "d"709: " "710: "s"711: "t"712: "y"713: "l"714: "e"715: "="716: """717: "p"718: "a"719: "d"720: "d"721: "i"722: "n"723: "g"724: "-"725: "l"726: "e"727: "f"728: "t"729: ":"730: " "731: "1"732: "e"733: "m"734: """735: ">"736: "<"737: "a"738: " "739: "h"740: "r"741: "e"742: "f"743: "="744: """745: "/"746: "d"747: "a"748: "t"749: "a"750: "/"751: "d"752: "a"753: "s"754: "h"755: "b"756: "o"757: "a"758: "r"759: "d"760: "/"761: "2"762: "."763: "j"764: "s"765: "o"766: "n"767: """768: ">"769: "2"770: "."771: "j"772: "s"773: "o"774: "n"775: "<"776: "/"777: "a"778: ">"779: "<"780: "/"781: "t"782: "d"783: ">"784: "<"785: "/"786: "t"787: "r"788: ">"789: "↵"790: "<"791: "t"792: "r"793: ">"794: "<"795: "t"796: "d"797: ">"798: "<"799: "c"800: "o"801: "d"802: "e"803: ">"804: "("805: "-"806: "r"807: "w"808: "-"809: "r"810: "w"811: "-"812: "r"813: "w"814: "-"815: ")"816: "<"817: "/"818: "c"819: "o"820: "d"821: "e"822: ">"823: "<"824: "/"825: "t"826: "d"827: ">"828: "<"829: "t"830: "d"831: " "832: "s"833: "t"834: "y"835: "l"836: "e"837: "="838: """839: "t"840: "e"841: "x"842: "t"843: "-"844: "a"845: "l"846: "i"847: "g"848: "n"849: ":"850: " "851: "r"852: "i"853: "g"854: "h"855: "t"856: ";"857: " "858: "p"859: "a"860: "d"861: "d"862: "i"863: "n"864: "g"865: "-"866: "l"867: "e"868: "f"869: "t"870: ":"871: " "872: "1"873: "e"874: "m"875: """876: ">"877: "<"878: "c"879: "o"880: "d"881: "e"882: ">"883: "3"884: "9"885: "3"886: "B"887: "<"888: "/"889: "c"890: "o"891: "d"892: "e"893: ">"894: "<"895: "/"896: "t"897: "d"898: ">"899: "<"900: "t"901: "d"902: " "903: "s"904: "t"905: "y"906: "l"907: "e"908: "="909: """910: "p"911: "a"912: "d"913: "d"914: "i"915: "n"916: "g"917: "-"918: "l"919: "e"920: "f"921: "t"922: ":"923: " "924: "1"925: "e"926: "m"927: """928: ">"929: "<"930: "a"931: " "932: "h"933: "r"934: "e"935: "f"936: "="937: """938: "/"939: "d"940: "a"941: "t"942: "a"943: "/"944: "d"945: "a"946: "s"947: "h"948: "b"949: "o"950: "a"951: "r"952: "d"953: "/"954: "3"955: "."956: "j"957: "s"958: "o"959: "n"960: """961: ">"962: "3"963: "."964: "j"965: "s"966: "o"967: "n"968: "<"969: "/"970: "a"971: ">"972: "<"973: "/"974: "t"975: "d"976: ">"977: "<"978: "/"979: "t"980: "r"981: ">"982: "↵"983: "<"984: "t"985: "r"986: ">"987: "<"988: "t"989: "d"990: ">"991: "<"992: "c"993: "o"994: "d"995: "e"996: ">"997: "("998: "-"999: "r"1000: "w"1001: "-"1002: "r"1003: "w"1004: "-"1005: "r"1006: "w"1007: "-"1008: ")"1009: "<"1010: "/"1011: "c"1012: "o"1013: "d"1014: "e"1015: ">"1016: "<"1017: "/"1018: "t"1019: "d"1020: ">"1021: "<"1022: "t"1023: "d"1024: " "1025: "s"1026: "t"1027: "y"1028: "l"1029: "e"1030: "="1031: """1032: "t"1033: "e"1034: "x"1035: "t"1036: "-"1037: "a"1038: "l"1039: "i"1040: "g"1041: "n"1042: ":"1043: " "1044: "r"1045: "i"1046: "g"1047: "h"1048: "t"1049: ";"1050: " "1051: "p"1052: "a"1053: "d"1054: "d"1055: "i"1056: "n"1057: "g"1058: "-"1059: "l"1060: "e"1061: "f"1062: "t"1063: ":"1064: " "1065: "1"1066: "e"1067: "m"1068: """1069: ">"1070: "<"1071: "c"1072: "o"1073: "d"1074: "e"1075: ">"1076: "1"1077: "."1078: "2"1079: "k"1080: "<"1081: "/"1082: "c"1083: "o"1084: "d"1085: "e"1086: ">"1087: "<"1088: "/"1089: "t"1090: "d"1091: ">"1092: "<"1093: "t"1094: "d"1095: " "1096: "s"1097: "t"1098: "y"1099: "l"1100: "e"1101: "="1102: """1103: "p"1104: "a"1105: "d"1106: "d"1107: "i"1108: "n"1109: "g"1110: "-"1111: "l"1112: "e"1113: "f"1114: "t"1115: ":"1116: " "1117: "1"1118: "e"1119: "m"1120: """1121: ">"1122: "<"1123: "a"1124: " "1125: "h"1126: "r"1127: "e"1128: "f"1129: "="1130: """1131: "/"1132: "d"1133: "a"1134: "t"1135: "a"1136: "/"1137: "d"1138: "a"1139: "s"1140: "h"1141: "b"1142: "o"1143: "a"1144: "r"1145: "d"1146: "/"1147: "d"1148: "a"1149: "s"1150: "h"1151: "b"1152: "o"1153: "a"1154: "r"1155: "d"1156: "."1157: "j"1158: "s"1159: "o"1160: "n"1161: """1162: ">"1163: "d"1164: "a"1165: "s"1166: "h"1167: "b"1168: "o"1169: "a"1170: "r"1171: "d"1172: "."1173: "j"1174: "s"1175: "o"1176: "n"1177: "<"1178: "/"1179: "a"1180: ">"1181: "<"1182: "/"1183: "t"1184: "d"1185: ">"1186: "<"1187: "/"1188: "t"1189: "r"1190: ">"1191: "↵"1192: "<"1193: "/"1194: "t"1195: "a"1196: "b"1197: "l"1198: "e"1199: ">"1200: "↵"1201: "<"1202: "b"1203: "r"1204: ">"1205: "<"1206: "a"1207: "d"1208: "d"1209: "r"1210: "e"1211: "s"1212: "s"1213: ">"1214: "N"1215: "o"1216: "d"1217: "e"1218: "."1219: "j"1220: "s"1221: " "1222: "v"1223: "0"1224: "."1225: "1"1226: "2"1227: "."1228: "5"1229: "/"1230: " "1231: "<"1232: "a"1233: " "1234: "h"1235: "r"1236: "e"1237: "f"1238: "="1239: """1240: "h"1241: "t"1242: "t"1243: "p"1244: "s"1245: ":"1246: "/"1247: "/"1248: "g"1249: "i"1250: "t"1251: "h"1252: "u"1253: "b"1254: "."1255: "c"1256: "o"1257: "m"1258: "/"1259: "j"1260: "e"1261: "s"1262: "u"1263: "s"1264: "a"1265: "b"1266: "d"1267: "u"1268: "l"1269: "l"1270: "a"1271: "h"1272: "/"1273: "n"1274: "o"1275: "d"1276: "e"1277: "-"1278: "e"1279: "c"1280: "s"1281: "t"1282: "a"1283: "t"1284: "i"1285: "c"1286: """1287: ">"1288: "e"1289: "c"1290: "s"1291: "t"1292: "a"1293: "t"1294: "i"1295: "c"1296: "<"1297: "/"1298: "a"1299: ">"1300: " "1301: "s"1302: "e"1303: "r"1304: "v"1305: "e"1306: "r"1307: " "1308: "r"1309: "u"1310: "n"1311: "n"1312: "i"1313: "n"1314: "g"1315: " "1316: "@"1317: " "1318: "l"1319: "o"1320: "c"1321: "a"1322: "l"1323: "h"1324: "o"1325: "s"1326: "t"1327: ":"1328: "8"1329: "0"1330: "0"1331: "0"1332: "<"1333: "/"1334: "a"1335: "d"1336: "d"1337: "r"1338: "e"1339: "s"1340: "s"1341: ">"1342: "↵"1343: "<"1344: "/"1345: "b"1346: "o"1347: "d"1348: "y"1349: ">"1350: "<"1351: "/"1352: "h"1353: "t"1354: "m"1355: "l"1356: ">"$promise: Promise$resolved: true__proto__: e
转化后的回复
e {response: "<!doctype html>↵<html>↵ <head>↵ <meta charset=…running @ localhost:8000</address>↵</body></html>", $promise: Promise, $resolved: true}$promise: Promise$resolved: trueresponse: "<!doctype html>↵<html>↵ <head>↵ <meta charset="utf-8">↵ <meta name="viewport" content="width=device-width">↵ <title>Index of /data/dashboard/</title>↵ </head>↵ <body>↵<h1>Index of /data/dashboard/</h1>↵<table><tr><td><code>(drw-rw-rw-)</code></td><td style="text-align: right; padding-left: 1em"><code></code></td><td style="padding-left: 1em"><a href="/data/dashboard/../">../</a></td></tr>↵<tr><td><code>(-rw-rw-rw-)</code></td><td style="text-align: right; padding-left: 1em"><code>393B</code></td><td style="padding-left: 1em"><a href="/data/dashboard/1.json">1.json</a></td></tr>↵<tr><td><code>(-rw-rw-rw-)</code></td><td style="text-align: right; padding-left: 1em"><code>394B</code></td><td style="padding-left: 1em"><a href="/data/dashboard/2.json">2.json</a></td></tr>↵<tr><td><code>(-rw-rw-rw-)</code></td><td style="text-align: right; padding-left: 1em"><code>393B</code></td><td style="padding-left: 1em"><a href="/data/dashboard/3.json">3.json</a></td></tr>↵<tr><td><code>(-rw-rw-rw-)</code></td><td style="text-align: right; padding-left: 1em"><code>1.2k</code></td><td style="padding-left: 1em"><a href="/data/dashboard/dashboard.json">dashboard.json</a></td></tr>↵</table>↵<br><address>Node.js v0.12.5/ <a href="https://github.com/jesusabdullah/node-ecstatic">ecstatic</a> server running @ localhost:8000</address>↵</body></html>"__proto__: e
答案 0 :(得分:4)
将您的dashboardResource.js更改为以下内容。默认情况下,$ query应返回list,但在你的情况下它必须是object,那么你需要为它设置配置。
-lubsan
如果从API
返回字符串,则转换您的响应myDash.factory("dashboardResource", function ($resource, $routeParams) {
return $resource("/data/dashboard/:id", {id: "@id"}, {
query: {method:'GET', isArray:false}
});
});
现在,您将在data.response中使用您的字符串。
FYI
这是任何$ rosource的默认配置。如果您想要进行任何更改,则需要覆盖。
myDash.factory("dashboardResource", function ($resource, $routeParams) {
return $resource("/data/dashboard/:id", {id: "@id"}, {
query: {method:'GET', isArray:false, transformResponse: function (data) {return {response: data}}
});
});
答案 1 :(得分:0)
return $resource("/data/dashboard/:id.json", {id: "@id"}, {
query: {method:'GET', isArray:false, transformResponse: function (data) {return {response: data}}
});
你需要添加&#39; .json&#39;在URL的末尾