我正在创建一个标志性应用程序,并且希望能够将JSON字符串从Web服务器加载到我的应用程序中。到目前为止,我只能加载一条消息。
![到目前为止,这是我的应用程序的样子。我只能加载一条消息] [1]
这是我的HTML代码:
<body>
<ion-view view-title="Announcements">
<ion-content ng-controller="Controller">
<ion-refresher
pulling-text="Pull to refresh..."
on-refresh="doRefresh()">
</ion-refresher>
<ion-list>
<ion-item>
<div class="list card">
<div class="item item-divider">{{announcement_name}} - {{date}}</div>
<div class="item item-body">
<div>
{{message}}
</div>
</div>
</div>
</ion-item>
</ion-list>
</ion-content>
</ion-view>
</body>
这是我的JavaScript控制器:
.controller('Controller', function($scope, $http) {
$scope.items = [1,2,3];
$scope.doRefresh = function() {
$http.get("")
.success(function(data) {
$scope.announcement_name = data.announcement_name;
$scope.date = data.date;
$scope.message = data.message;
})
.finally(function() {
$scope.$broadcast('scroll.refreshComplete');
});
};
});
这就是我的JSON文件的样子。如您所见,我有2个数组(消息),但只有一个正在加载。知道为什么吗?
[
{
"response1": {
"announcement_name": "ExampleMessage1",
"date": "26/05/2015",
"message": "ExampleMessage1"
},
"response2": {
"announcement_name": "ExampleMessage2",
"date": "27/05/2015",
"message": "ExampleMessage2"
}
}
]
您应该使用遍历json-array ng-repeat
。
这是一个例子:
$http.get("http://www.wikicode.co.uk/announcement.json")
.success(function(data) {
$scope.datafromservice = data;
})
.finally(function() {
...
});
在您的html中是这样的:
<div class="list card">
<div class="item item-divider" ng-repeat="message in data">{{message.announcement_name}} - {{messagedate}}</div>
</div>
这是ng-repeat的文档:https : //docs.angularjs.org/api/ng/directive/ngRepeat
希望这可以帮助。
更新
您可以将完整的json粘贴到var中。您的json可能看起来像这样(我正在开发的一个应用程序示例):
var reportapp = angular.module('reporting', []);
reportapp.controller('TreeController', ['$http', '$scope', function ($http, $scope) {
$scope.treedata = [];
$http.get('../data/tree.json').success(function (data) {
$scope.treedata = data;
});
//After the http-call your data should look something like this:
$scope.treedata = [
{
"name": "Titel",
"id": "1"
},
{
"name": "Allgemeine Standardangaben",
"id": "2"
},
{
"name": "Strategie und Analyse",
"id": "3",
"children": [
{
"name": "Erklärung des Entscheidungsträgers",
"id": "4"
},
{
"name": "Wichtigste Nachhaltigkeitsauswirkungen",
"id": "5"
}
]
}]
}]);
然后就可以ng-repeat
像这样访问数组:
<body ng-app="reporting">
<ul id="treecontainer" ng-controller="TreeController as treeCtrl">
<li ng-repeat="knoten in treedata" id="{{knoten.id}}">{{knoten.name}}
<ul ng-show="knoten.children">
<li ng-repeat="kind in knoten.children" id="{{kind.id}}">{{kind.name}}</li>
</ul>
</ul>
</body>
如果要显示子节点,可以使用ng-show
:
如果您有任何问题,请将其张贴在这里,我会尽力为您服务。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句