How to use ng-repeat on this JSON data - angularjs

I am trying to call hotel_name using ng-repeat. How do I do it.
This is the code. But nothing is being displayed.
<div ng-repeat="x in hotels">
<p ng-repeat="y in x.data">{{y.hotel_name}}</p>
</div>
This is the data format.
{
status: 200,
message: "Packages Found",
data: [
{
hotel_id: "40",
company_id: "2",
user_id: "17",
hotel_name: "Zen International",
hotel_description: "Good Hotel"
}
]
}

Your hotels seems an object. You don't need to iterate it.
Convert this
<div ng-repeat="x in hotels">
<p ng-repeat="y in x.data">{{y.hotel_name}}</p>
</div>
to this
<div>
<p ng-repeat="y in hotels.data">{{y.hotel_name}}</p>
</div>

You can us one ng-repeat
<div>
<p ng-repeat="y in hotels.data">{{y.hotel_name}}</p>
</div>
DEMO
var demoApp = angular.module('demoApp', []);
var controllers = {};
controllers.SimpleController = function ($scope) {
$scope.hotels = {
status: 200,
message: "Packages Found",
data: [
{
hotel_id: "40",
company_id: "2",
user_id: "17",
hotel_name: "Zen International",
hotel_description: "Good Hotel"
}
]
};
};
demoApp.controller(controllers);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.5/angular.min.js"></script>
<div ng-app="demoApp" ng-controller="SimpleController">
<div>
<p ng-repeat="y in hotels.data">{{y.hotel_name}}</p>
</div>
</div>

Bind Datalist with $scope in controller like
angular.module('YourApp', [])
.controller('YourCtrl', function($scope) {
$scope.dataList={
status: 200,
message: "Packages Found",
data: [
{
hotel_id: "40",
company_id: "2",
user_id: "17",
hotel_name: "Zen International",
hotel_description: "Good Hotel"
}
]
}
});
and bind dataListwith view like
<div ng-app="YourApp" ng-controller="YourCtrl">
<div>
<p ng-repeat="item in dataList.data">{{item.hotel_name}}</p>
</div>
</div>

Related

best practice and performance in angularJS ng-if with class and icon

2 options:
what is better for performance and watchers using, now I am using the first option and I would like to improve performance
the object today looks like:
message = {
message : X,
}
and I would like to do somthing like :
obj = {
text : text,
icon: "src.png"
status: X,
class : "className",
color: "color_code_like_#ffff"
}
1 :
<div ng-if="message.message == 0" class="classA" style="">
<span class="same"><img class="sameClass" ng-src="a.gif"></span>
<span class="status-text a_with_animation" style="color:red;">textA</span>
</div>
<div ng-if="message.message == 1" class="classB" style="">
<span class="same"><img class="sameClass" ng-src="b.png"></span>
<span class="status-text" style="color:blue;">textB</span>
</div>1
<div ng-if="message.message == 2" class="classC" style="">
<span class="same"><img class="sameClass" ng-src="c.png"></span>
<span class="status-text" style="color:black;">TextC</span>
</div>
option 2
<div class="{{obj.class}}" style="">
<span class="same"><img class="sameClass" ng-src="{{obj.class}}"></span>
<span class="status-text {{obj.animation}" style="color:red;">
{{obj.text}}</span>
</div>
also all the data here is two way binding
Not really pretty but I made an example of how you could populate your data with ng-include template. Here is a demo:
<!DOCTYPE html>
<html ng-app="myApp">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body>
<div ng-controller="myCtrl">
<select ng-model="message" ng-change="change()" ng-options="x.message for x in messages">
</select>
<br>
ngInclude: <div ng-include="'abc.html'"></div>
Directive: <div temp-directive></div>
<!-- A separate file for the template -->
<script type="text/ng-template" id="abc.html">
<div class="{{obj.class}}" style="">
<span class="same"><img class="sameClass" ng-src="{{obj.icon}}"></span>
<span class="status-text {{obj.animation}}" ng-style="obj.style">
{{obj.text}}</span>
</div>
</script>
</div>
<script>
var app = angular.module('myApp', []);
app.directive("tempDirective", function() {
return {
template : "<div class='{{obj.class}}' style=''><span class='same'><img class='sameClass' ng-src='{{obj.icon}}'></span><span class='status-text {{obj.animation}}' ng-style='obj.style'> {{obj.text}}</span></div>"
};
});
app.controller('myCtrl', function($scope) {
$scope.messages = [{
"message": 0
}, {
"message": 1
}, {
"message": 2
}];
$scope.message = $scope.messages[0]; // initialise
$scope.objects = [{
"text": "Message - 0",
"icon": "a.gif",
"animation": "a_with_animation",
"class": "classA",
"style": {
"color": "#00aaaa"
}
}, {
"text": "Message - 1",
"icon": "b.png",
"animation": "",
"class": "classB",
"style": {
"color": "#aa00aa"
}
}, {
"text": "Message - 2",
"icon": "c.png",
"animation": "",
"class": "classC",
"style": {
"color": "#aaaa00"
}
}];
$scope.obj = $scope.objects[0]; // initialise
$scope.change = function() { // changing the template data
$scope.obj = $scope.objects[$scope.message.message];
}
});
</script>
</body>
</html>
[Note]: it is best to replace ng-include with a component / directive with their template (not templateUrl) for performance improvement, since it is asynchronous and takes time to load the HTML

How to show glyphicon eye based on value in Angularjs?

I have glyphicon-eye for all fields in the form. If user click on glyphicon-eye-open then it will change to glyphicon-eye-close and I push that specific field name into an array.
In my JSON response I am getting the hidden field values but how can I use that value and call exact glyphicon-eye.
JSON response :
{
"response": {
"status": {
"code": "0",
"message": "Success"
},
"service": {
"servicetype": "4",
"functiontype": "1005"
},
"data": {
"session_id": "372",
"roles": [
{
"hiddenfields": [
{
"fname": "firstname",
"fblink": "fblink",
"country": "country",
"martialStatus": "martialStatus"
}
]
}
]
}
}
}
Controller :
$scope.user = {
fname: "firstname",
lname: "lastname",
dob: "dob",
gender: "gender",
country: "country",
state: "state",
city: "city",
pincode: "pincode",
martialStatus: "martialStatus",
emailId: "emailId",
mobile: "mobile",
qualification: "qualification",
fblink: "fblink"
};
$scope.allow = {};
$scope.users = [];
$scope.push = function(){
$scope.users = [];
var user = {},
allow = $scope.allow;
console.log(allow);
Object.keys(allow).forEach(function(key){
allow[key] ? user[key] = $scope.user[key] : null;
});
$scope.users.push(user);
}
HTML :
<a class="menu-toggle" class="btn btn-default" ng-model="allow.fname"><i class="glyphicon" ng-class="{'glyphicon-eye-open':allow.fname, 'glyphicon-eye-close':!allow.fname}" ng-click="push(allow.fname = allow.fname?false:true)"></i></a>
If field value is in array then I need to show glyphicon-eye-close.
You can use ng-class like below.
<div class="form-group" ng-repeat="x in allow" >
<button class="btn btn-default"><span class="glyphicon" ng-class="{ 'glyphicon-eye-open': x.fname==0 , 'glyphicon-eye-close': x.fname==1}"></span> {{x.name}}</button>
</div>
function myCtrl($scope) {
$scope.allow=[{
'fname':1,
'name':'Anil'
},{
'fname':0,
'name':'Sunil'
},{
'fname':1,
'name':'Manil'
}]
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div ng-app ng-controller="myCtrl" class="col-md-12">
<div class="form-group" ng-repeat="x in allow" >
<button class="btn btn-default"><span class="glyphicon" ng-class="{ 'glyphicon-eye-open': x.fname==0 , 'glyphicon-eye-close': x.fname==1}"></span> {{x.name}}</button>
</div>
</div>
Some times the ! doesnot works inside ng-clss. You please replace that by
<a class="menu-toggle" class="btn btn-default" ng-model="allow.fname">
<i class="glyphicon"
ng-class="{'glyphicon-eye-open':allow.fname.length > 0, 'glyphicon-eye-close':allow.fname.length == 0}"
ng-click="push(allow.fname = allow.fname?false:true)">
</i>
</a>
I am now able to glyphicon based on values by assigning the response to my model as below,
$scope.allow = response.data.roles[0].hiddenfields[0];

Loop through the JSON file which has a unique ID with ng-repeat

I have trouble looping a JSON file. Ng-repeat through the data array where each field has a unique id.
var fullData = {
data: [{
505: {
price: "505",
source: "Amazon"
},
456: {
price: "4555",
source: "Jet"
}
}]
}
<div ng-repeat="data in fullData.data">
{{ data[$index]}}
</div>
This solution dosent seem to work.
Try like this.
var app = angular.module('app',[])
app.controller('ctrl',function($scope){
$scope.data = {
data: [{
505: {
price: "505",
source: "Amazon"
},
456: {
price: "4555",
source: "Jet"
}
}]
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
<div ng-repeat="(key,value) in data.data">
<div ng-repeat="(k,v) in value">
{{v.price}}:
{{v.source}}
</div>
</div>
</div>
You should use $scope variable in controller to bind the data, change your code like this,
Controller
app.controller("DemoCtrl", ["$scope",
function($scope) {
$scope.fullData = {
data: [{
505: {
price: "505",
source: "Amazon"
},
456: {
price: "4555",
source: "Jet"
}
}]
}
}
]);
HTML
<body ng-app='app'>
<div class="media-list" ng-controller="DemoCtrl">
<div ng-repeat=" data in fullData.data ">
{{data}}
</div>
</div>
</body>
DEMO
as I mentioned in comment dont use
var fulData
use
$scope.fullData
this will automatically bind the value in view
as you did
{{ data[$index]}}
is not the way to access in ng-repeat data itself is enough to access as like this
<div ng-repeat="data in fullData.data">
{{$index}}
{{ data}}
</div>
here is js code
$scope.fullData = {
data: [{
505: {
price: "505",
source: "Amazon"
},
456: {
price: "4555",
source: "Jet"
}
}]
}
working demo http://codepen.io/vkvicky-vasudev/pen/yaEEyv

ng-repeat in other ng-repeat is not working

{
"data": {
"name": "NAME",
"tests": [
{
"testname": "abc",
"relatedResource": [
{
"accessType": "fttb",
},
{
"accessType": "fttn",
}
]
},
{
"testname": "xyz",
"relatedResource": [
{
"accessType": "fttp",
},
{
"accessType": "fttp",
}
]
}
]
}
}
Controller
$scope.data=response.data.tests;
<div ng-repeat="l in data">
<div ng-repeat="i in l.relatedResource">
{{i.accessType}}
</div>
</div>
try this, you need multiple ng-repeat to access relatedResource
here I printed the output
<div ng-controller="MyCtrl">
<div ng-repeat="(key,val) in data">
{{val.name}}
<div ng-repeat="test in val.tests">
- {{test.testname}}
<div ng-repeat="resource in test.relatedResource">
- -{{resource.accessType}}
</div>
</div>
</div>
</div>
Try this snippet
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.data = {
"data": {
"name": "NAME",
"tests": [
{
"testname": "abc",
"relatedResource": [
{
"accessType": "fttb",
},
{
"accessType": "fttn",
}
]
},
{
"testname": "xyz",
"relatedResource": [
{
"accessType": "fttp",
},
{
"accessType": "fttp",
}
]
}
]
}
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
<div ng-repeat="obj in data">
<span ng-repeat="val in obj.tests">
{{val.testname}}
<span ng-repeat="v in val.relatedResource">
{{v.accessType}}
</span>
<br/>
</span>
</div>
</div>
There is actually not much information about the question.
If you receive the json-data from a remote server, you should always check and convert it to a real json-object.
You can simply use
$scope.data = response.data.tests;
$scope.getData = function()
{
return angular.toJson($scope.data);
}
or you can test it with:
{{data | json}}

AngularJS : `Load more` button in each group of ng-repeat with many groups titles

I want to add load more button to the bottom of each group like the image here, and after clicking the button it shows rest of parts of the relevant group
where a,g are groups titles those have group property
In the snippet bellow, the code return only one load more button, and with no consideration of the group property.
var myApp = angular.module('myApp',[]);
myApp.controller('main', ['$scope', function($scope) {
$scope.test = "test";
$scope.filteredModules = [
{
"name":"a",
"group":"a"
},
{
"name":"b",
},
{
"name":"c",
},
{
"name":"c",
},
{
"name":"e",
},
{
"name":"f",
},
{
"name":"g",
"group":"g"
}
,{
"name":"h",
},
{
"name":"i",
},
{
"name":"j",
},
{
"name":"k",
}
,
{
"name":"l",
}
];
$scope.limit = 4;
$scope.loadMore = function() {
var increamented = $scope.limit + 4;
$scope.limit = increamented > $scope.filteredModules.length ? $scope.filteredModules.length : increamented;
};
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
<div ng-controller="main">
<div ng-repeat="node in filteredModules | limitTo:limit track by $index">
<div ng-if="!node.group">{{node.name}}</div>
<div ng-if="node.group" style="background-color:red">{{node.name}} </div>
</div>
<button ng-click="loadMore()">Load more</button>
</div>
</div>
Please try this snippet
var myApp = angular.module('myApp',[]);
myApp.controller('main', ['$scope', function($scope) {
$scope.filteredModules = {
"groups":
[
{
"title": "Alfreds Futterkiste",
"childs": ["child-1", "child-2", "child-3", "child-4", "child-5", "child-6","child-7", "child-8", "child-9"],
"limit": "3"
},
{
"title": "Ana Trujillo Emparedados y helados",
"childs": ["child-1", "child-2", "child-3", "child-4", "child-5", "child-6"],
"limit": "4"
},
{
"title": "Antonio Moreno Taquería",
"childs": ["child-1", "child-2", "child-3", "child-4", "child-5", "child-6"],
"limit": "3"
}
]
};
$scope.loadMore = function(index) {
$scope.filteredModules.groups[index].limit = parseInt($scope.filteredModules.groups[index].limit) + 3;
}
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
<div ng-controller="main">
<div ng-repeat="node in filteredModules.groups track by $index">
<div ng-if="node.title" style="background-color:red">{{node.title}}
</div>
<div ng-repeat="child in node.childs | limitTo: node.limit">
{{child}}
</div>
<br/>
<button ng-click="loadMore($index)" ng-hide="node.limit >= node.childs.length">Load more</button>
<br/><br/>
</div>
</div>
</div>

Resources