show data from nested JSON array - angularjs

how to show "city" from this JSON file through angularJS http:
{"ers":{"resMessage":"1","response":"Success","data":{"alarms":[{"id":"1","alarm_id":"2","title":"Fire","description":"fire","type":"Fire","priority":"High","address":"Kanpur, Uttar Pradesh, India","city":"Kanpur","state":"UP","country":"India","zipcode":"123456","lattitude":"26.449923","longitude":"80.3318736"},{"id":"3","alarm_id":"4","title":"test-02","description":"test-02","type":"Medical","priority":"High","address":"Borivali West, Mumbai, Maharashtra, India","city":"Mumbai","state":"MH","country":"India","zipcode":"123456","lattitude":"19.2461644","longitude":"72.85090560000003"}]}}}

Step 1:
Call url and get json data in $scope.data variable and use ng repeat in your template
Step 2:
Use angular for each for get city in controller
Refer following example
angular.module('myApp',[]).controller('myCtrl', function($scope){
$scope.data = {
"ers": {
"resMessage": "1",
"response": "Success",
"data": {
"alarms": [{
"id": "1",
"alarm_id": "2",
"title": "Fire",
"description": "fire",
"type": "Fire",
"priority": "High",
"address": "Kanpur, Uttar Pradesh, India",
"city": "Kanpur",
"state": "UP",
"country": "India",
"zipcode": "123456",
"lattitude": "26.449923",
"longitude": "80.3318736"
}, {
"id": "3",
"alarm_id": "4",
"title": "test-02",
"description": "test-02",
"type": "Medical",
"priority": "High",
"address": "Borivali West, Mumbai, Maharashtra, India",
"city": "Mumbai",
"state": "MH",
"country": "India",
"zipcode": "123456",
"lattitude": "19.2461644",
"longitude": "72.85090560000003"
}]
}
}
};
angular.forEach($scope.data.ers.data.alarms, function(value) {
console.log(value.city)
});
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
<div ng-repeat="obj in data.ers.data.alarms">
City:- {{obj.city}}<br>
Address:- {{obj.address}}
</div>
</div>
Hope this will help you

You can use ng-repeat over myData.ers.data.alarms
DEMO
angular.module('myApp',[]).controller('myCtrl', function($scope){
$scope.myData = {"ers":{"resMessage":"1","response":"Success","data":{"alarms":[{"id":"1","alarm_id":"2","title":"Fire","description":"fire","type":"Fire","priority":"High","address":"Kanpur, Uttar Pradesh, India","city":"Kanpur","state":"UP","country":"India","zipcode":"123456","lattitude":"26.449923","longitude":"80.3318736"},{"id":"3","alarm_id":"4","title":"test-02","description":"test-02","type":"Medical","priority":"High","address":"Borivali West, Mumbai, Maharashtra, India","city":"Mumbai","state":"MH","country":"India","zipcode":"123456","lattitude":"19.2461644","longitude":"72.85090560000003"}]}}};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
<div ng-repeat="data in myData.ers.data.alarms">
<h1> {{data.city}}</h1>
</div>
</div>

Related

to make able list item show their details on click

I am trying to get the latitude and longitude of the cities according to id...for example if i will click on kanpur it will show its latitude which is returning from API.But what i did is returning latitude of only last city which is returning from API.
my app.js code is:
.controller('AppCtrl', function($scope,
$http,$ionicModal,$cordovaGeolocation, $ionicLoading, $ionicPlatform) {
$scope.currentItem = 1;
$scope.latitude;
$scope.longitude;
$scope.ers = {
'agency_device_id' : ''
};
$scope.submit = function(){
var link = 'http://trendytoday.in/ers/api/DeviceAlarms';
$http.post(link, {ers: $scope.ers},{headers: {'Content-Type': 'application/json'} }).then(function (res){
// $scope.mssg = res.data.ers.resMessage;
// $scope.resp = res.data.ers.response;
$scope.arr = [];
angular.forEach(res.data.ers.data.alarms, function(value) {
$scope.arr.push(value);
//console.log(value.city)
latitude=value.lattitude;
longitude=value.longitude;
})
});
}
$scope.getdetails = function(){
window.alert(latitude);
};
});
and API response is:
{
"ers": {
"resMessage": "1",
"response": "Success",
"data": {
"alarms": [
{
"id": "1",
"alarm_id": "2",
"title": "Fire",
"description": "fire",
"type": "Fire",
"priority": "High",
"address": "Kanpur, Uttar Pradesh, India",
"city": "Kanpur",
"state": "UP",
"country": "India",
"zipcode": "123456",
"lattitude": "26.449923",
"longitude": "80.3318736"
},
{
"id": "3",
"alarm_id": "4",
"title": "test-02",
"description": "test-02",
"type": "Medical",
"priority": "High",
"address": "Borivali West, Mumbai, Maharashtra, India",
"city": "Mumbai",
"state": "MH",
"country": "India",
"zipcode": "123456",
"lattitude": "19.2461644",
"longitude": "72.85090560000003"
}
]
}
}
}
Assuming your $scope.currentItem is the id of the selected city such as kanpur, then when the API returns the data, check for this value to match the id and set the $scope variables.
I hope you get the idea.
function MyCtrl($scope) {
$scope.currentItem = 1;
$scope.latitude;
$scope.longitude;
$scope.submit = function() {
$scope.res = {
data: {
"ers": {
"resMessage": "1",
"response": "Success",
"data": {
"alarms": [{
"id": "1",
"alarm_id": "2",
"title": "Fire",
"description": "fire",
"type": "Fire",
"priority": "High",
"address": "Kanpur, Uttar Pradesh, India",
"city": "Kanpur",
"state": "UP",
"country": "India",
"zipcode": "123456",
"lattitude": "26.449923",
"longitude": "80.3318736"
},
{
"id": "3",
"alarm_id": "4",
"title": "test-02",
"description": "test-02",
"type": "Medical",
"priority": "High",
"address": "Borivali West, Mumbai, Maharashtra, India",
"city": "Mumbai",
"state": "MH",
"country": "India",
"zipcode": "123456",
"lattitude": "19.2461644",
"longitude": "72.85090560000003"
}
]
}
}
}
}
$scope.arr = [];
angular.forEach($scope.res.data.ers.data.alarms, function(value) {
$scope.arr.push(value);
if ($scope.currentItem == value.id) {
$scope.latitude = value.lattitude;
$scope.longitude = value.longitude;
}
});
}
$scope.submit();
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app ng-controller="MyCtrl">
Long: {{longitude}}
Lati: {{latitude}}
</body>
the only thing i changed is i passed obj as a parameter to getdetails function.
$scope.getdetails = function(obj){
alert(obj.lattitude);
}
and in html:
<div ng-repeat="obj in arr " ng-click="getdetails(obj)">
<p><b>{{obj.city}}</b></p>
</div>

Fatching JSON data into angularjs

I am trying to read data from json file into angular.js but i am not getting data.
[
{
"code": "AD",
"name": "Andorra",
"population": "84000"
},
{
"code": "TN",
"name": "Tunisia",
"population": "10589025"
},
{
"code": "TW",
"name": "Taiwan",
"population": "22894384"
},
{
"code": "TZ",
"name": "Tanzania",
"population": "41892895"
},
{
"code": "UA",
"name": "Ukraine",
"population": "45415596"
},
{
"code": "UG",
"name": "Uganda",
"population": "33398682"
},
{
"code": "UM",
"name": "U.S. Minor Outlying Islands",
"population": "0"
},
{
"code": "US",
"name": "United States",
"population": "310232863"
},
{
"code": "UY",
"name": "Uruguay",
"population": "3477000"
},
{
"code": "UZ",
"name": "Uzbekistan",
"population": "27865738"
},
{
"code": "VA",
"name": "Vatican City",
"population": "921"
},
{
"code": "VC",
"name": "Saint Vincent and the Grenadines",
"population": "104217"
},
{
"code": "VE",
"name": "Venezuela",
"population": "27223228"
},
{
"code": "VG",
"name": "British Virgin Islands",
"population": "21730"
},
{
"code": "VI",
"name": "U.S. Virgin Islands",
"population": "108708"
},
{
"code": "VN",
"name": "Vietnam",
"population": "89571130"
},
{
"code": "VU",
"name": "Vanuatu",
"population": "221552"
},
{
"code": "WF",
"name": "Wallis and Futuna",
"population": "16025"
},
{
"code": "WS",
"name": "Samoa",
"population": "192001"
},
{
"code": "ZW",
"name": "Zimbabwe",
"population": "11651858"
}
]
index.html
<html ng-app="countryApp">
<head>
<meta charset="utf-8">
<title>Angular.js JSON Fetching Example</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
<script>
var countryApp = angular.module('countryApp', []);
countryApp.controller('CountryCtrl', function ($scope, $http){
$http.get('countries.json').success(function(data) {
$scope.countries = data;
});
});
</script>
</head>
<body ng-controller="CountryCtrl">
<h2>Angular.js JSON Fetching Example</h2>
<table>
<tr>
<th>Code</th>
<th>Country</th>
<th>Population</th>
</tr>
<tr ng-repeat="country in countries">
<td>{{country.code}}</td>
<td>{{country.name}}</td>
<td>{{country.population}}</td>
</tr>
</table>
</body>
</html>
when you make an $http.get call, the callback function returns the response object. This means that you need to access the data property of the response, not just assign the response:
$http.get('countries.json').success(function(response) {
$scope.countries = response.data;
});
take a look at the docs for $http.get, specifically the example ($http docs)
Please check the path of the json file because the code it's fine.
If you are executing the code from your local machine you need a server to run the code like nodejs.

How can I view a selected item in detailpage using angularjs factory $http and $stateParameters

Here is mij code but data is hardcoded and working perfect. When I try my data dynamicly calling data responsed good. My question is look this script with hardcoded data. If i will selected item viewing in detailpage working nice.
Shortly this hardcoded data must from webmethod. How can I do it?
This is my html code for view menu as list;
<div class="row">
<div class="col-lg-12">
<ol class="breadcrumb">
<li class="active">
<i class="fa fa-dashboard"></i> Menu
</li>
</ol>
<div class="row" ng-controller="DesktopController">
<div class="col-lg-10">
<h2>Menu Lijst</h2>
<div class="table-responsive">
<table class="table table-bordered table-hover table-striped">
<thead>
<tr>
<th>ID</th>
<th>Description</th>
<th>State</th>
<th>ParentID</th>
<th>Parent</th>
<th>MenuItem ID</th>
<th>Index</th>
<th></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="menu in List" style="width:auto;">
<td>{{menu.Description}}</td>
<td>{{menu.State}}</td>
<td>{{menu.ParentID}}</td>
<td>{{menu.Parent}}
</td>
<td>{{menu.MenuItemID}}</td>
<td>{{$index}}</td>
<td>
View Detail
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
Here is my detail menu view page...
<h1>View DetailMenu Page</h1>
<br />
<!--<h1>{{menu.Description}}</h1>-->
<div class="row" ng-controller="viewDetailMenuController">
<div class="col-lg-10">
<h2>{{menu.Description}}</h2>
</div>
</div>
App.factory('menuService', function ($http) {
var obj = {
getAllMenus: [{ "ID": "1", "Description": "MOTOR", "State": "1", "ParentID": "0", "Parent": "", "MenuItemID": "0" }, { "ID": "2", "Description": "FILTERS", "State": "0", "ParentID": "1", "Parent": "MOTOR", "MenuItemID": "1" }, { "ID": "3", "Description": "BRANDSTOF/TANKDOPPEN", "State": "0", "ParentID": "1", "Parent": "MOTOR", "MenuItemID": "2" }, { "ID": "4", "Description": "ONSTEKING", "State": "0", "ParentID": "1", "Parent": "MOTOR", "MenuItemID": "3" }, { "ID": "5", "Description": "ACCU/STARTEN", "State": "0", "ParentID": "1", "Parent": "MOTOR", "MenuItemID": "4" }, { "ID": "6", "Description": "BOUGIES/BOUGIEKABELS", "State": "0", "ParentID": "1", "Parent": "MOTOR", "MenuItemID": "5" }, { "ID": "7", "Description": "D-RIEMEN/V-REIM-SETS", "State": "0", "ParentID": "1", "Parent": "MOTOR", "MenuItemID": "6" }, { "ID": "8", "Description": "PAKKINGEN", "State": "0", "ParentID": "1", "Parent": "MOTOR", "MenuItemID": "7" }, { "ID": "9", "Description": "MOTORBLOK", "State": "0", "ParentID": "1", "Parent": "MOTOR", "MenuItemID": "8" }, { "ID": "10", "Description": "CYLINDERKOP", "State": "0", "ParentID": "1", "Parent": "MOTOR", "MenuItemID": "9" }, { "ID": "11", "Description": "MOTORMANAGEMENT", "State": "0", "ParentID": "1", "Parent": "MOTOR", "MenuItemID": "10" }, { "ID": "12", "Description": "UITLATEN", "State": "0", "ParentID": "1", "Parent": "MOTOR", "MenuItemID": "11" }, { "ID": "13", "Description": "KOELING", "State": "2", "ParentID": "0", "Parent": "", "MenuItemID": "12" }, { "ID": "14", "Description": "AIRCO", "State": "0", "ParentID": "13", "Parent": "KOELING", "MenuItemID": "13" }, { "ID": "15", "Description": "KOELSYSTEEM/WATERPOMP", "State": "0", "ParentID": "13", "Parent": "KOELING", "MenuItemID": "14" }, { "ID": "16", "Description": "SCHAKELAARS/SENSOREN", "State": "0", "ParentID": "13", "Parent": "KOELING", "MenuItemID": "15" }, { "ID": "17", "Description": "SLANGEN/LEIDINGEN", "State": "0", "ParentID": "13", "Parent": "KOELING", "MenuItemID": "16" }, { "ID": "18", "Description": "ONDERSTEL", "State": "1", "ParentID": "0", "Parent": "", "MenuItemID": "17" }, { "ID": "19", "Description": "STUURDELEN", "State": "0", "ParentID": "19", "Parent": "STUURDELEN", "MenuItemID": "18" }, { "ID": "20", "Description": "KABELS", "State": "0", "ParentID": "19", "Parent": "STUURDELEN", "MenuItemID": "19" }, { "ID": "21", "Description": "AANDRIJVING/HOEAEN", "State": "0", "ParentID": "19", "Parent": "STUURDELEN", "MenuItemID": "20" }, { "ID": "22", "Description": "KOPPELING", "State": "0", "ParentID": "19", "Parent": "STUURDELEN", "MenuItemID": "21" }, { "ID": "23", "Description": "WIELLAGERS", "State": "0", "ParentID": "19", "Parent": "STUURDELEN", "MenuItemID": "22" }, { "ID": "24", "Description": "SCHOKDEMPERS", "State": "0", "ParentID": "19", "Parent": "STUURDELEN", "MenuItemID": "23" }, { "ID": "25", "Description": "VEREN", "State": "0", "ParentID": "19", "Parent": "STUURDELEN", "MenuItemID": "24" }, { "ID": "26", "Description": "TREKHAAK", "State": "0", "ParentID": "19", "Parent": "STUURDELEN", "MenuItemID": "25" }, { "ID": "27", "Description": "BANDEN", "State": "0", "ParentID": "19", "Parent": "STUURDELEN", "MenuItemID": "26" }, { "ID": "28", "Description": "REMDELEN NAT/DROOG", "State": "2", "ParentID": "0", "Parent": "", "MenuItemID": "27" }, { "ID": "29", "Description": "REM-DELEN/SCHIJVEN", "State": "0", "ParentID": "28", "Parent": "REMDELEN NAT/DROOG", "MenuItemID": "28" }]
}
return obj;
});
Controllers:
var App = angular.module("app", ['ui.router']);
App.config(function ($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/home');
$stateProvider
.state('home', {
url: '/home',
templateUrl: 'Pages/Home.html'
})
.state('menu', {
url: '/menu',
templateUrl: 'Pages/Menu.html'
})
.state('menuDetail', {
url: '/menuDetail/{id}',
templateUrl: 'Pages/MenuDetail.html',
controller: 'viewDetailMenuController'
});
});
App.run(function ($rootScope, $state) {
$rootScope.$state = $state;
});
App.controller("DesktopController", function ($scope, menuService) {
$scope.List = menuService.getAllMenus;
});
App.controller("viewDetailMenuController", function ($scope, menuService, $stateParams) {
$scope.menu = menuService.getAllMenus[$stateParams.id];
});
This work perfect. Only my question is how can I do that selected menu view in detail page using $http $stateParams (dynamic data calling from webservice or webmethod)?
Before this question I have shared calling data from webmethod you can see it.
Thanks...
write this code
.state('menuDetail', {
url: '/menuDetail/:id',
templateUrl: 'Pages/MenuDetail.html',
controller: 'viewDetailMenuController'
});
<a ui-sref="menuDetail({ id: dynamicId })">Click</a>
and when you click on 'Click' then open detail page
For more know https://github.com/angular-ui/ui-router/wiki/quick-reference
Let's say you want to get your menu items from server instead of hardcoding.
App.factory('menuService', function ($http) {
var service = {};
service.getAllMenus = function(){
return $http.get('url_or_your_webapi');//this returns a promise
};
return service;
});
then we need to get that menu data in controller:
App.controller("DesktopController", function ($scope, menuService) {
// here we set callback for promise we received from our menu service
menuService.getAllMenus().then(function(response){$scope.List = response.data;});
});
UPD menu service code fixed, now returns function

ng-repeat & objects relationship

So I made a webshop with firebase, and I want each product to have its own review section, and to keep it optimized I have to keep my data as flat as possible.
Consider this database structure
Lets say, after I repeat my 7 products. I want to repeat all their corresponding reviews in them, I'm new to angular & really can't figure it out.
Check out this jsfiddle which is an example of my data & lays down what I want to achieve.
// the main (app) module
var myApp = angular.module("myApp", []);
// add a controller
myApp.controller("showProducts", function($scope) {
//Products
$scope.products = [{
"brand": "Apple",
"id": 0,
"img": "img/macbook.png",
"model": 2015,
"name": "Macbook 13'",
"operatingSystem": "OS X",
"price": "1900"
}, {
"brand": "Google",
"id": 1,
"img": "img/chromebook.png",
"model": "Pixel 2",
"name": "Chromebook",
"operatingSystem": "Chrome OS",
"price": "1700"
}, {
"brand": "Dell",
"id": 2,
"img": "img/dellxps13.png",
"model": "XPS13",
"name": "Dell XP13",
"operatingSystem": "Win 10",
"price": "1500"
}, {
"brand": "Lenovo",
"id": 3,
"img": "img/lenovo.png",
"model": "A2",
"name": "Lenovo A2",
"operatingSystem": "Win 7",
"price": "999"
}, {
"brand": "Sony",
"id": 4,
"img": "img/vaio.png",
"model": "VAIO",
"name": "Sony vaio",
"operatingSystem": "Win 8.0",
"price": "1200"
}, {
"brand": "Acer",
"id": 5,
"img": "img/acer.png",
"model": "a7",
"name": "Acer A7",
"operatingSystem": "Win 8.1",
"price": "1199"
}, {
"brand": "AW",
"id": 6,
"img": "http://gearnuke.com/wp-content/uploads/2015/07/alienware-1-1.jpg",
"model": "5A",
"name": "Alienware",
"operatingSystem": "Windows 10",
"price": "5000"
}];
//Reviews
$scope.reviews = [{
"-K1bfGsnWOclf1R85Uec": {
"comment": "This laptop is great",
"rating": "5",
"user": "Maryam"
}
}, {
"-K1bg0wHIUQ9qbxspLmg": {
"comment": "This laptop is working good so far!",
"rating": "4",
"user": "spongbob"
},
"-K1bzqAQpr1br6h04bSH": {
"comment": "some comment",
"rating": "10",
"user": "test"
}
}, {
"-K1bfvS9MYGE82nTx67r": {
"comment": "This laptop is meh",
"rating": "3",
"user": "superman"
}
}, {
"-K1bgSTfbjQ-LRDScH-3": {
"comment": "daym son",
"rating": "10",
"user": "Omar"
}
}, {
"-K1bgljvaeY94E_aPQvY": {
"comment": "commenthere",
"rating": "10",
"user": "pewdiepie"
}
}, {
"-K1bgmXWai9PDake2Wh1": {
"comment": "commenthere",
"rating": "10",
"user": "tiesto"
}
}, {
"-K1bgnMlsp_Dl5ooBl2G": {
"comment": "commenthere",
"rating": "10",
"user": "ex-gf"
},
"-K1gxHCtPdEniOGNFcWi": {
"comment": "idk",
"rating": 3,
"user": "idk"
}
}];
});
.card {
margin: 1% 1% 1% 1%;
padding: 1% 1% 1% 1%;
width: 250px;
height: 150px;
overflow-y: auto;
border: 1px solid black;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myApp" ng-controller="showProducts">
<div class="card" ng-repeat="product in products">
<h5>{{ product.name }}</h5>
</div>
<div ng-repeat="review in reviews">
<div ng-repeat="(key, data) in review">User:{{ data.user }} | Comment:{{ data.comment }} | Rating:{{ data.rating }}</div>
</div>
</body>
At the bottom of my results, you will see all the comments. I have tried many things to get them in the right places without much success.
You can use $index to grab the correct list of reviews per product. You'll only need two ng-repeat.
// the main (app) module
var myApp = angular.module("myApp", []);
// add a controller
myApp.controller("showProducts", function($scope) {
//Products
$scope.products = [{
"brand": "Apple",
"id": 0,
"img": "img/macbook.png",
"model": 2015,
"name": "Macbook 13'",
"operatingSystem": "OS X",
"price": "1900"
}, {
"brand": "Google",
"id": 1,
"img": "img/chromebook.png",
"model": "Pixel 2",
"name": "Chromebook",
"operatingSystem": "Chrome OS",
"price": "1700"
}, {
"brand": "Dell",
"id": 2,
"img": "img/dellxps13.png",
"model": "XPS13",
"name": "Dell XP13",
"operatingSystem": "Win 10",
"price": "1500"
}, {
"brand": "Lenovo",
"id": 3,
"img": "img/lenovo.png",
"model": "A2",
"name": "Lenovo A2",
"operatingSystem": "Win 7",
"price": "999"
}, {
"brand": "Sony",
"id": 4,
"img": "img/vaio.png",
"model": "VAIO",
"name": "Sony vaio",
"operatingSystem": "Win 8.0",
"price": "1200"
}, {
"brand": "Acer",
"id": 5,
"img": "img/acer.png",
"model": "a7",
"name": "Acer A7",
"operatingSystem": "Win 8.1",
"price": "1199"
}, {
"brand": "AW",
"id": 6,
"img": "http://gearnuke.com/wp-content/uploads/2015/07/alienware-1-1.jpg",
"model": "5A",
"name": "Alienware",
"operatingSystem": "Windows 10",
"price": "5000"
}];
//Reviews
$scope.reviews = [{
"-K1bfGsnWOclf1R85Uec": {
"comment": "This laptop is great",
"rating": "5",
"user": "Maryam"
}
}, {
"-K1bg0wHIUQ9qbxspLmg": {
"comment": "This laptop is working good so far!",
"rating": "4",
"user": "spongbob"
},
"-K1bzqAQpr1br6h04bSH": {
"comment": "some comment",
"rating": "10",
"user": "test"
}
}, {
"-K1bfvS9MYGE82nTx67r": {
"comment": "This laptop is meh",
"rating": "3",
"user": "superman"
}
}, {
"-K1bgSTfbjQ-LRDScH-3": {
"comment": "daym son",
"rating": "10",
"user": "Omar"
}
}, {
"-K1bgljvaeY94E_aPQvY": {
"comment": "commenthere",
"rating": "10",
"user": "pewdiepie"
}
}, {
"-K1bgmXWai9PDake2Wh1": {
"comment": "commenthere",
"rating": "10",
"user": "tiesto"
}
}, {
"-K1bgnMlsp_Dl5ooBl2G": {
"comment": "commenthere",
"rating": "10",
"user": "ex-gf"
},
"-K1gxHCtPdEniOGNFcWi": {
"comment": "idk",
"rating": 3,
"user": "idk"
}
}];
});
.card {
margin: 1% 1% 1% 1%;
padding: 1% 1% 1% 1%;
width: 250px;
height: 150px;
overflow-y: auto;
border: 1px solid black;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myApp" ng-controller="showProducts">
<div class="card" ng-repeat="product in products">
<h5>{{ product.name }}</h5>
<div ng-repeat="(key, data) in reviews[$index]">User:{{ data.user }} | Comment:{{ data.comment }} | Rating:{{ data.rating }}</div>
</div>
</body>
I want to repeat all their corresponding reviews
There must be a relationship between your products and their reviews. So try to add product id to the reviews for mapping.
Once you have a mapping between product and their respective reviews you can for something like below with filter to achieve your functionality.
<div ng-repeat="product in products">
<div class="card"><h5>{{ product.name }}</h5></div>
<div ng-repeat="review in reviews | filter:{productId: product.id}"">
<div ng-repeat="(key, data) in review">User:{{ data.user }} | Comment:{{ data.comment }} | Rating:{{ data.rating }}</div>
</div>
</div>

AngularJS variable scope

I am sure I am missing some small detail. I have a factory which reads data from the server and retrieves JSON results. Then, in my controller I do the following:
.controller('homeController', ['$scope','RecentItems', function($scope,RecentItems)
{
//scopes for the items
$scope.items={};
RecentItems.getRecentItems().done(function(data){
$scope.items = data;
console.log($scope.items);
});
}])
In the HTML template i have an ng-repeat directive > ng-repeat="item in items"
And the results don't appear in the view. console.log($scope.items) displays the data in the console however, in the view the items appears to be empty.
I know that it has to do with the variable scopes in JavaScript rather than being an AngularJS problem
THIS IS THE JSON RESULT:
[
{
"iid": "32",
"name": "Womens bag 2",
"brand": "Gucci",
"category": "Handtaschen",
"minprice": "20",
"retailprice": "400",
"duedate": "2015-06-10 21:25:00",
"description": "second bag",
"img": "images/items/1433274066696.jpg",
"current_price": "0"
},
{
"iid": "31",
"name": "Womens Bag",
"brand": "Gucci",
"category": "Handtaschen",
"minprice": "20",
"retailprice": "300",
"duedate": "2015-06-10 21:25:00",
"description": "Bag",
"img": "images/items/1433274042147.jpg",
"current_price": "0"
},
{
"iid": "30",
"name": "Watch2",
"brand": "Swiss",
"category": "Uhren",
"minprice": "20",
"retailprice": "200",
"duedate": "2015-06-10 21:25:00",
"description": "watch to test",
"img": "images/items/1433273997525.png",
"current_price": "0"
},
{
"iid": "29",
"name": "Watch",
"brand": "Swiss",
"category": "Uhren",
"minprice": "10",
"retailprice": "200",
"duedate": "2015-06-10 21:25:00",
"description": "Test",
"img": "images/items/1433273879811.png",
"current_price": "0"
}
]

Resources