我正在学习 AngularJS。
当我想访问 xml 文件时,我无法做到这一点,我使用了 xml_strjson 此方法将数据 xml 转换为 json 但无法在 html 文件中访问 这是我的代码:
enter code here<!DOCTYPE html>
<html ng-app="">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"> </script>
<script type="text/javascript" src="http://demos.amitavroy.com/learningci/assets /js/xml2json.js" charset="UTF-8"></script>
<script>
function customersController($scope, $http) {
$http.get("http://localhost:13087/Data.xml")
.success(function (response) {
$scope.lstComapanies = x2js.xml_str2json(response);
console.log($scope.lstComapanies);
alert(response);
});
}
</script>
<title></title>
</head>
<body ng-controller="customersController">
<table>
<tbody>
<tr ng-repeat="CompanyModel in lstComapanies">
<!-- $index for duplicate values -->
<td class="border">
<label>{{ CompanyModel }}</label>
</td>
</tr>
</tbody>
</table>
我的 json 数据
{
"__cnt": 8,
"CompanyModel": [
{
"__cnt": 7,
"BacklogSum": "646",
"BacklogSum_asArray": [
"646"
],
"BillingSum": "607",
"BillingSum_asArray": [
"607"
],
"BookingSum": "646",
"BookingSum_asArray": [
"646"
],
"CashflowSum": "$-4809038.45",
"CashflowSum_asArray": [
"$-4809038.45"
],
"LstCompanies": {
"__cnt": 1,
"_i:nil": "true"
},
"LstCompanies_asArray": [
{
"__cnt": 1,
"_i:nil": "true"
}
],
"Name": "OPTERNA AM, INC. ",
"Name_asArray": [
"OPTERNA AM, INC. "
],
"Number": "2000",
"Number_asArray": [
"2000"
]
}
],
"_xmlns:i": "http://www.w3.org/2001/XMLSchema-instance",
}
最佳答案
您需要为您的应用创建一个主模块,然后向其中添加 Controller 。
首先,将脚本更改为:
var myApp = angular.module('myApp', []);
myApp.controller('customersController', ['$scope', '$http',
function($scope, $http){
$http.get("http://localhost:13087/Data.xml")
.success(function (response) {
$scope.lstComapanies = x2js.xml_str2json(response);
console.log($scope.lstComapanies);
alert(response);
})
}]);
然后正确设置您的应用和 Controller
<body ng-app="myApp">
<div ng-controller="customersController">
<table>
.... etc
此外,如上所述,您需要更改 ng-repeat。
<tr ng-repeat="objects in lstComapanies.CompanyModel">
关于c# - xml转json后无法访问数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27525546/