【发布时间】:2015-09-25 12:35:23
【问题描述】:
我正在 ionic 框架中开发一个原型应用程序。我是 Angular js、HTML、CSS、Java Script 和所有这些东西的新手。
我有一个 json 文件用作输入。我能够解析这个 Json 文件并能够从中获取 json 对象。此 json 对象包含项目数组。您可以参考下面的 json 内容。这里的项目是应用程序A,B.....
更新输入 Json:
{
"data": [
{
"applicationname": "A",
"permissions": [
{
"text": "at"
},
{
"text": "at1"
}
]
},
{
"applicationname": "B",
"permissions": [
{
"text": "bt"
},
{
"text": "bt1"
}
]
}
]
}
当应用程序第一次加载时,应用程序应该只加载上面 json 数组中的第一项,这意味着只加载应用程序"A"(第一项)数据。
一旦用户单击页脚中的任何按钮(安装/取消),它就会更改其数据并显示application "B" 的内容。这个过程应该一直持续到 json 数组的末尾。
我当前的代码甚至没有加载第一个项目数据。我在 HTML 中做错了吗?
更新代码:
HTML 文件:
<ion-header-bar class="bar-calm">
<h1 class="title">Application Permissions</h1>
</ion-header-bar>
<ion-nav-view name="home" ng-repeat="app in applicationdata">
<div class="bar bar-subheader bar-positive">
<h3 class="title"> {{app.applicationname }}</h3>
</div>
<ion-content class="has-subheader">
<div class="list" ng-controller="CheckboxController">
<ion-checkbox ng-repeat="item in app.permissions" ng-model="item.checked" ng-checked="selection.indexOf(item) > -1" ng-click="toggleSelection(item)">
{{ item.text }}
<h3 class="item-text-wrap"> details come soon </h3>
</ion-checkbox>
<div class="item">
<pre ng-bind="selection | json"></pre>
</div>
<div class="item">
<pre ng-bind="selection1 | json"></pre>
</div>
</div>
</ion-content>
<ion-footer-bar align-title="left" class="bar-light" ng-controller="FooterController">
<div class="buttons">
<button class="button button-balanced" ng-click="infunc()"> Install </button>
</div>
<h1 class="title"> </h1>
<div class="buttons" ng-click="doSomething()">
<button class="button button-balanced"> Cancel </button>
</div>
</ion-footer-bar>
</ion-nav-view>
app.js 文件:
pmApp.controller('CheckboxController', function ($scope, $http, DataService) {
// define the function that does the ajax call
getmydata = function () {
return $http.get("js/sample.json")
.success(function (data) {
$scope.applicationdata = data;
});
}
// do the ajax call
getmydata().success(function (data) {
// stuff is now in our scope, I can alert it
$scope.data = $scope.applicationdata.data;
$scope.devList = $scope.data[0].permissions;
console.log("data : " + JSON.stringify($scope.data));
console.log("first application data : " + JSON.stringify($scope.devList));
});
$scope.selection = [];
$scope.selection1 = [];
// toggle selection for a given employee by name
$scope.toggleSelection = function toggleSelection(item) {
var idx = $scope.selection.indexOf(item);
var jsonO = angular.copy(item);
jsonO.timestamp = Date.now();
DataService.addTrackedData(jsonO);
$scope.selection1 = DataService.getTrackedData();
// is currently selected
if (idx > -1) {
$scope.selection.splice(idx, 1);
}
// is newly selected
else {
DataService.addSelectedData(item);
$scope.selection = DataService.getSelectedData();
/* $scope.selection.push(item);*/
}
};
});
问题:
1 : 为什么第一项的数据没有被加载?根据我的理解,我已经对 HTML 进行了更改。
2:如何浏览所有项目。我会尝试@John Carpenter 的回答。在解决第一个问题之前。
请帮助我,提前谢谢。
【问题讨论】:
-
为什么关闭?这有什么问题?
-
demo 当它所做的只是抛出错误时毫无价值。有问题的邮政编码不是图片。另外问题是什么?如果您想重复项目,请使用
ng-repeat,这是每个角度基础教程的一部分 -
@charlietfl 感谢您的评论。我知道如何使用 ng-repeat,这里我的问题是不同的。当用户单击安装/取消按钮时,我想继续从 json 数组中一一显示项目。
标签: javascript json angularjs ionic-framework