0

I am developing one prototype application in ionic framework. I am newbie for angular js, HTML, CSS , Java Script and all this stuff.

I have one json file which I am using as an input. I am able to parse this Json file and able to get json object from this. This json object contains array of items. You can refer below json content for this. Here items are application A,B.....

Updated Input Json :

{
    "data": [
        {
            "applicationname": "A",
            "permissions": [
                {
                    "text": "at"
                },
                {
                    "text": "at1"
                }
            ]
        },
        {
            "applicationname": "B",
            "permissions": [
                {
                    "text": "bt"
                },
                {
                    "text": "bt1"
                }
            ]
        }
    ]
}

When the application loads for the first time, application should load only the first item from above json array which means only application "A" (first item) data.

Once user clicks on any button (install/cancel) in Footer then it should changed its data and display application "B"'s contents. This process should continue till the end of json array.

My current code is not loading even the first item data in. Am I doing something wrong in HTML?

Updated Code :

HTML file :

<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 file :

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);*/
        }
    };

});

Problems :

1 : Why is the data of first item not getting loaded? I have done changes in HTML as per my understanding.

2 : How Can I navigate through all items. I will try @John Carpenter's answer. Before that first problem should be resolved.

Please help me, thanks in advance.

sam_k
  • 5,983
  • 14
  • 76
  • 110
  • Why closed? Whats wrong with this? – sam_k Jul 07 '15 at 20:32
  • 1
    I've answered a couple of your questions related to this program of yours. I would suggest reading [this page](http://stackoverflow.com/help/how-to-ask) article. The key points here that I think you're missing are _Introduce the problem before you post any code_, _Write a title that summarizes the specific problem_, and _Help others reproduce the problem_. For this last part, look [here](http://stackoverflow.com/help/mcve). This question is long, it is not clear what you are asking, and it will not be helpful to future people visiting the site. – Frank Bryce Jul 07 '15 at 21:17
  • 1
    demo is worthless when all it does is throw errors. Post code in question not images. Also ***what is the question?*** If you want repeating items, use `ng-repeat` which is part of every angular basic tutorial – charlietfl Jul 07 '15 at 23:27
  • @charlietfl thanks for your comment. I know how to use ng-repeat, Here my question is different. I want to keep showing items one by one from json array when user click on install/cancel button. – sam_k Jul 08 '15 at 01:21

1 Answers1

2

OK, so I'm not 100% sure what you want but I'll take a stab at it. In the future, it would be helpful to post less code (probably not the entire project you are working on). It is a good idea to make a simpler example than the "real" one, where you can learn what you need to learn and then go apply it to the "real" code that you have.

Anyways, this example is a simple button that you click on to change what is displayed.

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>
Frank Bryce
  • 8,076
  • 4
  • 38
  • 56
  • Thanks for your answer. I am not sure what information is not clear for you guys. I will try to make simple demo and will update my question – sam_k Jul 08 '15 at 14:26
  • My main question: what is the _key_ part of your project that you would like to solve with your question? In general, a specific project will not be useful to future visitors of stackoverflow, however a specific _problem_ could be. In my answer I attempt to solve the _problem_ of displaying different information from an object on the screen on a button click, which is what I thought your _problem_ might be. I don't think fixing your specific project code is helpful to the stackoverflow community. – Frank Bryce Jul 08 '15 at 14:30
  • I understand what are you trying to say. I will edit question soon. Give me some time to make it working as demo – sam_k Jul 08 '15 at 14:31
  • Check my updated question. I refer this question's answer in my case. http://stackoverflow.com/questions/24293300/angular-js-click-through-json-data-array But I am not able to see data. – sam_k Jul 10 '15 at 17:21
  • I dnt think so, Here you can take a look on my console log :http://pastebin.com/KJEJ4MdQ – sam_k Jul 10 '15 at 18:22
  • any idea how to do? or you require more explanation for it? – sam_k Jul 11 '15 at 15:02
  • Yes it will be, I have not checked before that I am not able to load first item data when first time page loads. From jsonArray it should load first item data. you can see in my HTML file I have done properly but I am not able to see on page. I hope you got clear idea about first problem. If problem first solved then I can apply your solution. – sam_k Jul 11 '15 at 19:14
  • Please see this question : http://stackoverflow.com/questions/24293300/angular-js-click-through-json-data-array I have applied same solution but not able to see my data at very first time – sam_k Jul 11 '15 at 19:15
  • Have you seen my updated question #1. I am trying since last 3 days. I am totally stuck here. Can you please help me. – sam_k Jul 12 '15 at 14:52
  • It is impossible for me to reproduce your problem unless you either give me a link to your entire code base (not a good option) or you reduce your problem to the simplest amount of code possible in order to reproduce the issue you are having. Again, I'll link [this article](http://stackoverflow.com/help/mcve) for helpful information on this. To me, this means getting rid of ALL graphical considerations (for your data problem), and trying to properly get your data from your DataService. – Frank Bryce Jul 12 '15 at 17:37
  • Can you please take a look on this question ? http://stackoverflow.com/questions/31975132/open-new-page-when-user-click-on-any-item-from-ion-item – ojas Aug 13 '15 at 02:03