I'm a newcomer to using AngularJS
and I have an ambition to create an eCommerce website that showcases recipes. I want to fetch all the JSON
data related to recipes and display detailed information in grid boxes, similar to what's shown in this example image here. However, I've encountered a problem where the order of recipes appears to be random each time the page is loaded, resulting in different recipes being displayed within the block intended for showcasing 8 recipes. It seems like there's an issue with my JavaScript
code, but I'm struggling to figure out how to organize these recipes correctly.
Below is the HTML file:
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0-beta.2/angular-route.min.js"></script>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="H:/job/Xmapp/htdocs/AngularJs/recipesController3.js"></script>
<link rel="stylesheet" href="H:/job/Xmapp/htdocs/AngularJs/imgStyle.css">
</head>
<body>
<div ng-app="myApp" ng-controller="mainController" class="center">
<div class="container-fluid">
<div class="row">
<div class="col-sm-3" ng-repeat="recipes in listOfrecipes |limitTo:8 track by $index">
<div class="panel panel-default">
<div class="panel-body">
<div class="name"><h4> {{ recipes.Recipe.name|uppercase}}</h4>
<p>4 Serves</p>
<h4>MAIN INGREDIENT :</h4>
<table class="table_style">
<tr>
<td>- {{recipes.IngredientMapping[0].Ingredient.name}}</td>
<td>- {{recipes.IngredientMapping[1].Ingredient.name}}</td>
</tr>
<tr>
<td>- {{recipes.IngredientMapping[2].Ingredient.name}}</td>
<td>- {{recipes.IngredientMapping[3].Ingredient.name}}</td>
</tr>
</table>
<br>
<div>
{{recipes.Recipe.directions|limitTo:100}}
<a href="/" class="dotStyle"><strong>....</strong></a>
</div>
<div>
<img class="img" ng-src="http://164.132.196.117/chop_dev/recipe/files/image/attachment/{{recipes.Image[0].id}}/{{recipes.Image[0].attachment}}">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<br>
</body>
</html>
And here is my Controller.js file:
var myApp = angular.module('myApp', []);
myApp.controller('mainController', function ($scope, $http) {
console.log('dddddd');
// delete $http.defaults.headers.common['X-Requested-With'];
$scope.listOfRecipe = null;
$scope.listOfIngredient = Array();
$scope.listOfrecipes = Array();
var url = "http://164.132.196.117/chop_dev/recipe/recipes.json";
var url2 =
"http://164.132.196.117/chop_dev/recipe/recipes/view/";
function first_call() {
return $http({
method: 'GET',
url: url
}).then(function (response) {
var wait_it = false;
$scope.listOfRecipe = response.data.recipes;
//to get how many recipes in total in json file
console.log($scope.listOfRecipe);
var recipeLength = $scope.listOfRecipe.length;
$scope.listOfIngredient = new Array(recipeLength);
for (var j = 0; j < 100; j++) {
$scope.listOfIngredient[j] = Array();
}
console.log(recipeLength);
for (var i = 0; i < recipeLength; i++) {
//access to different individual recipe with recipe id
another_call($scope.listOfRecipe[i].Recipe.id);
}
});
}
function another_call(Recipeid) {
$http.post(url2 + Recipeid + ".json", null).then(function (response2) {
var one_recipe = response2.data.recipe
$scope.listOfrecipes.push(one_recipe);
});
console.log($scope.listOfrecipes);
}
first_call();
});