0

Here is a project I am working on for fun trying to get used to the ng-repeat directive. I created the following so far.

Its working but I am attempting to add more to this to make it work better. I have been trying to add either buttons or a drop down menu to select part of my JSON to do the ng-repeat on.

In my example I have workout days with exercises.

I would like a drop down menu showing the days One thing I have been researching and haven't found much that is doing this.

Basically I want it to load the exercises from the JSON based on the selection of the drop down days menu all coming from my JSON file.

PLUNKER: http://plnkr.co/edit/IRis4l3r0MVCjpLNd3Q3

var app = angular.module('app', []).config(function($sceDelegateProvider) {
$sceDelegateProvider.resourceUrlWhitelist([
// Allow same origin resource loads.
'self',
'http://*.bodybuilding.com/**'
]);
});
app.controller('homeCtrl', function($scope) {


$scope.data = {JSON FILE}

$scope.exercises = [];
angular.forEach($scope.data.day, function(day, index) {
  console.log(day);
  angular.forEach(day.exercises, function(exercise, index){
    //console.log(posters);
    $scope.exercises.push(exercise);
  });
});
console.log($scope.data);

});

*Any help would be appreciated.

1 Answer 1

0

I forked your plunker and the dropdown is now organized by your day array in $scope.data, and inside each dropdown item it is repeated each exercise name.

You could check it here:

http://plnkr.co/edit/DoIBDv2w0vY4zAWSq89D?p=preview

//Important html code
<li><a href="#" ng-repeat="day in data.day" ng-click="selectedDay($index)">day {{$index}}  |  <span ng-repeat="exercise in day.exercises">{{exercise.name}}</span></a></li>

Try clicking on a dropdown element.

Basically, you need a ng-repeat inside a ng-repeat, since you are iterating first over the days, and for each day you want to interate over it's exercises.

Hope I helped :)

Sign up to request clarification or add additional context in comments.

4 Comments

I see that does help out I am going to keep playing with it and see what else I can do with it I would like to update the second ng-repeat based off the selection using the ng-click directive and see what I can make!
You could just create a new array with the index you selected: $scope.new_array = $scope.data.day[index]; and use this array to show the selected data.
Have any more knowledge I have it displaying in my console view but am struggling understand how exactly I am displaying it from a foreach. plnkr.co/edit/S84lzY7T57afbkBRlBOK
I've edited your plunker, you can see it working here: plnkr.co/edit/Gvw7IzzDucDOzc78pBLN?p=preview Your ng-repeat was not repeating over your new created $scope.day array. Also, you should clear the array every time you call renderPage function. Hope I helped :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.