简体   繁体   English

Ionic / Angular:如何浏览嵌套的json

[英]Ionic/Angular: How to navigate through nested json

iam currently working on my first angular/ionic app. iam目前正在开发我的第一个角度/离子应用程序。

I have a big json file: 我有一个很大的json文件:

userWorkouts: [{
  title: '3 Split',
  id: 1,
  workoutImg: 'img.jpg',
  workoutSessions: [{
    workoutSessionName: 'Monday',
    workoutSessionColor: "#000000",
    workoutIcon: "icon-monday",
    workoutExerciseList: [{
      exerciseName: "Pull Ups",
      exerciseSets: [20, 12, 8]
    }, {
      exerciseName: "Push Ups",
      exerciseSets: [1, 2, 8]
    }]
  }, {
    workoutSessionName: 'Wednesday',
    workoutSessionColor: "#FFFFFF",
    workoutIcon: "icon-wednesday",
    workoutExerciseList: [{
      exerciseName: "Trizep",
      exerciseSets: [20, 12, 8]
    }, {
      exerciseName: "Xyz",
      exerciseSets: [1, 2, 8]
    }]
  }]
}

Now i want to create multiple views for each section of the workout. 现在,我想为锻炼的每个部分创建多个视图。

Workout Name -> Workout Sessions (Weekdays) -> Exercises on this day. 锻炼名称->锻炼时间(平日)->当天的锻炼。

So far i got something like this: 到目前为止,我有这样的事情:

<ion-item ng-repeat="workout in workoutList" class="workout-list-item item item-thumbnail-left" ui-sref="app.workoutSessionList({workoutIndex: $index})">
    <img class="" src="{{workout.workoutImg}}">
    <h2>{{workout.title}}</h2>
    <ion-option-button class="button-positive icon ion-edit" ng-click="deleteUserWorkout(workout)"></ion-option-button>
    <ion-option-button class="button-assertive icon ion-trash-a" ng-click="deleteUserWorkout(workout)"></ion-option-button>

  </ion-item>

But this doenst work for me. 但这确实对我有用。

Have you any ideas whats the best way to navigate through this json file? 您有什么想法浏览此json文件的最佳方法是什么?

Thanks a lot! 非常感谢!

First of all JSON Object is broken. 首先,JSON对象已损坏。 ( I mean after you GET json file in JS code ) (我的意思是在您用JS代码获取json文件之后)

If userWorkouts is child of workoutList  Then you should use 
workout in workoutList.userWorkouts in ng-repeat

Else JSON object should have been userWorkouts = [{}]; = inplace of :

Dealing with nested JSON is simple. 处理嵌套JSON很简单。 All you need to do is to make sure that your templates matches the JSON structure. 您需要做的就是确保模板与JSON结构匹配。

In your case, your model is actually an array, and it is nested with arrays, so the template that we need to build is a nested foreach template. 在您的情况下,您的模型实际上是一个数组,并且与数组嵌套,因此我们需要构建的模板是嵌套的foreach模板。 I will be leaving out workoutSessionColor and workoutIcon in this example. 在此示例中,我将省略executionSessionColorexecutionIcon but i am sure you will figure out how to utilize those information. 但我相信您会弄清楚如何利用这些信息。

Go ahead and play with it on codepen. 继续并在Codepen上玩它。 The following code snippets matches what's been posted on code pen. 以下代码段与代码笔上发布的内容匹配。

Sample Html based on your json: 基于json的示例HTML:

<div ng-app="myApp" ng-controller="TestController">

  <div ng-repeat="workout in userWorkouts">

    <p ng-bind="workout.title"></p>
    <img src="{{workout.workoutImg}}">

    <table border=1>
      <tr ng-repeat="workoutDays in workout.workoutSessions">
        <td ng-bind="workoutDays.workoutSessionName"></td>
        <td>
          <p ng-repeat="sessionExercise in workoutDays.workoutExerciseList">
            {{sessionExercise.exerciseName}} | {{sessionExercise.exerciseSets}}
          </p>
        </td>
      </tr>
    </table>

  </div>

</div>

Sample css (trivial): 示例CSS(简单):

img{
  width:50px;
}

Angular code: 角度代码:

var app = angular.module("myApp", []);

app.controller("TestController", function($scope){
  $scope.userWorkouts= [{
    title: '3 Split',
    id: 1,
    workoutImg: 'https://cdn3.iconfinder.com/data/icons/workouts/500/push_up-512.png',
    workoutSessions: [{
      workoutSessionName: 'Monday',
      workoutSessionColor: "#000000",
      workoutIcon: "icon-monday",
      workoutExerciseList: [{
        exerciseName: "Pull Ups",
        exerciseSets: [20, 12, 8]
      }, {
        exerciseName: "Push Ups",
        exerciseSets: [1, 2, 8]
      }]
    }, {
      workoutSessionName: 'Wednesday',
      workoutSessionColor: "#FFFFFF",
      workoutIcon: "icon-wednesday",
      workoutExerciseList: [{
        exerciseName: "Trizep",
        exerciseSets: [20, 12, 8]
      }, {
        exerciseName: "Xyz",
        exerciseSets: [1, 2, 8]
      }]
    }]
  }];
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM