我正在学习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",
}
答案 0 :(得分:3)
您需要为您的应用创建一个主模块,然后将控制器添加到它。
首先,将脚本更改为:
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);
})
}]);
然后正确设置您的应用和控制器
<body ng-app="myApp">
<div ng-controller="customersController">
<table>
.... etc
另外,如上所述,您需要更改ng-repeat。
<tr ng-repeat="objects in lstComapanies.CompanyModel">
答案 1 :(得分:0)
很简单:
<tr ng-repeat="CompanyModel in lstComapanies.CompanyModel">
查看您的JSON。它以具有两个属性的对象开头: 1. ____cnt = 8(听起来像计数) 2. CompanyModel =对象数组
所以转发器需要一个数组,你给了他一个对象。但是这个对象包括你的数组。
JSON表示Java Script Obect Notation,当您在JavaScript中解析JSON时,它最终会出现在一个对象或数组中。所以你可以直接访问你的lstComapanies的所有东西(应该是lstCampanies?)。
如果不能正常工作那么做:
使用控制器并迭代数据
angular.module('myApp', []) .controller('customersController', ['$scope', '$http', $scope.lstComapanies = {}; function($scope, $http){ $http.get("http://localhost:13087/Data.xml") .success(function (response) { $scope.lstComapanies = JSON.parse(x2js.xml_str2json(response)); console.log($scope.lstComapanies); alert(response); }) }]); <body ng-app="myApp"> <div ng-controller="customersController"> ... <tr ng-repeat="CompanyModel in lstComapanies.CompanyModel"> <td>{{ CompanyModel.__cnt }}</td> <td>{{ CompanyModel.BacklogSum }}</td> <td> <span ng-repeat="sum in CompanyModel.BacklogSum_asArray">{{ sum }}</span> </td> </tr> ... </div>
注意:所有浏览器中都不存在JSON.parse(),请参见此处:Safely turning a JSON string into an object
但是,我会将http get东西放在工厂或服务类中,这可以注入任何需要访问数据的控制器。接下来我会答应哪些成功通过getCompanies()
等特定的API函数返回所需的数据