【问题标题】:How to show item from json Array one by one in Angular JS如何在Angular JS中一一显示json数组中的项目
【发布时间】: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 的回答。在解决第一个问题之前。

请帮助我,提前谢谢。

【问题讨论】:

  • 为什么关闭?这有什么问题?
  • 我已经回答了你的几个与你的这个程序有关的问题。我建议阅读this page 文章。我认为您缺少的关键点是在发布任何代码之前介绍问题写一个总结特定问题的标题,以及帮助他人重现问题。对于最后一部分,请查看here。这个问题很长,不清楚你在问什么,对以后访问该网站的人没有帮助。
  • demo 当它所做的只是抛出错误时毫无价值。有问题的邮政编码不是图片。另外问题是什么?如果您想重复项目,请使用ng-repeat,这是每个角度基础教程的一部分
  • @charlietfl 感谢您的评论。我知道如何使用 ng-repeat,这里我的问题是不同的。当用户单击安装/取消按钮时,我想继续从 json 数组中一一显示项目。

标签: javascript json angularjs ionic-framework


【解决方案1】:

好的,所以我不能 100% 确定您想要什么,但我会尝试一下。将来,发布更少的代码会很有帮助(可能不是您正在处理的整个项目)。制作一个比“真实”示例更简单的示例是个好主意,您可以在其中学习需要学习的内容,然后将其应用到您拥有的“真实”代码中。

无论如何,这个示例是一个简单的按钮,您可以单击它来更改显示的内容。

var app = angular.module('MyApplication',[]);

app.controller('MyController', ['$scope', function($scope){
    $scope.indexToShow = 0;
    $scope.items = [
        'item 1', 
        'item 2', 
        'item 3'
    ];
    
    $scope.change = function(){
        $scope.indexToShow = ($scope.indexToShow + 1) % $scope.items.length;
    };
}]);
.simple-button {
  cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="MyApplication" ng-controller="MyController">
  <div ng-repeat="item in items track by $index" ng-show="$index == indexToShow">
    {{item}}
  </div>
  <div class="simple-button" ng-click="change()">click me!</div>
</div>

【讨论】:

  • 感谢您的回答。我不确定你们不清楚哪些信息。我将尝试制作简单的演示并更新我的问题
  • 我的主要问题是:您想用您的问题解决项目的关键部分是什么?一般来说,一个特定的项目不会对未来的 stackoverflow 访问者有用,但是一个特定的问题可能是有用的。在我的回答中,我试图解决在单击按钮时从屏幕上的对象显示不同信息的问题,这就是我认为您的问题 可能存在的问题。我认为修复您的特定项目代码对 stackoverflow 社区没有帮助。
  • 我明白你想说什么。我会尽快编辑问题。给我一些时间让它作为演示工作
  • 检查我更新的问题。在我的情况下,我参考了这个问题的答案。 stackoverflow.com/questions/24293300/… 但是我看不到数据。
  • 我不这么认为,您可以在这里查看我的控制台日志:pastebin.com/KJEJ4MdQ
猜你喜欢
  • 1970-01-01
  • 2021-05-24
  • 1970-01-01
  • 1970-01-01
  • 2021-07-30
  • 2020-06-05
  • 1970-01-01
  • 1970-01-01
  • 2019-06-02
相关资源
最近更新 更多