Nested ng-repeat with AngularJS - javascript

I cannot seem to get out the values I need from a nested ng-repeat in AngularJS.
I have the following data sent from the server:
[{"Your Turn":[{"gameToken":"sSFK0OHhYa8M4F6Z5Z1uNFnawQNVkZXj1GZyTEeSv2h8gntr9UrEU5FQsR9r8YWMWvjA6tePINbOm2Fc5u8U659QaRK0dju0","friendId":"522eec17e4b088a0a939cfdb","friendName":"Michelle","status":"You are waiting for Michelle Miguelamimio Agoramarketomercado to accept your challenge"}]},{"Their Turn":[{"gameToken":"sSFK0OHhYa8M4F6Z5Z1uNFnawQNVkZXj1GZyTEeSv2h8gntr9UrEU5FQsR9r8YWMWvjA6tePINbOm2Fc5u8U659QaRK0dju0","friendId":"522eec17e4b088a0a939cfdb","friendName":"Michelle Miguelamimio Agoramarketomercado","status":"You are waiting for Michelle to accept your challenge"}]}]
And the following template for the HTML:
<div ng-repeat="turn in data">
<p>{{turn}}</p>
<ul class="list-bare list-inline tab-group">
<li ng-repeat="(key, value) in turn" class="tab tab-active txt-upper {{key | lowercase}}">{{key}}</li>
</ul>
<ul class="list-bare list-users clearfix">
<li class="clearfix" ng-repeat="game in turn">
<div class="avatar-container">
<img class="avatar" ng-src="{{baseUrl}}/shobo/user/avatar/{{game.friendId}}" alt="{{game.friendName}}" />
</div>
<div class="list-users-info">
<h3 class="text-cap">{{game.friendName}}</h3>
<p>{{game.status}}</p>
</div>
<div class="list-users-status txt-center" ng-click="acceptChallenge(game.gameToken,game.friendId)">
<div><i class="icon-chevron-right"></i></div>
</div>
</li>
</ul>
</div>
I am not seeing any of the 'game' values being output from the second ng-repeat and am not sure what i am doing wrong?

<li class="clearfix" ng-repeat="game in turn">
should be
<li class="clearfix" ng-repeat="game in value">

need to make some changes in data and html to make require html
$scope.data=[{
"Title":"Your Turn",
"Turn": [{
"gameToken":"sSFK0OHhYa8M4F6Z5Z1uNFnawQNVkZXj1GZyTEeSv2h8gntr9UrEU5FQsR9r8YWMWvjA6tePINbOm2Fc5u8U659QaRK0dju0",
"friendId": "522eec17e4b088a0a939cfdb",
"friendName": "Michelle",
"status": "You are waiting for Michelle Miguelamimio Agoramarketomercado to accept your challenge"
}]
}, {
"Title":"Their Turn",
"Turn": [{
"gameToken": "sSFK0OHhYa8M4F6Z5Z1uNFnawQNVkZXj1GZyTEeSv2h8gntr9UrEU5FQsR9r8YWMWvjA6tePINbOm2Fc5u8U659QaRK0dju0",
"friendId": "522eec17e4b088a0a939cfdb",
"friendName": "Michelle Miguelamimio Agoramarketomercado",
"status": "You are waiting for Michelle to accept your challenge"
}]
}];
HTML
<div ng-repeat="turn in data">
<ul class="list-bare list-users clearfix">
<h1>{{turn.Title}}</h1>
<li class="clearfix" ng-repeat="game in turn.Turn">
<div class="avatar-container">
<img class="avatar" ng-src="{{baseUrl}}/shobo/user/avatar/{{game.friendId}}" alt="{{game.friendName}}" />
</div>
<div class="list-users-info">
<h3 class="text-cap">{{game.friendName}}</h3>
<p>{{game.status}}</p>
</div>
<div class="list-users-status txt-center" ng-click="acceptChallenge(game.gameToken,game.friendId)">
<div><i class="icon-chevron-right"></i></div>
</div>
</li>
</ul>
</div>
please refer below fiddle
http://jsfiddle.net/U3pVM/1476/

Related

Ng-repeat & Ng-bind-html

Does ng-repeat works inside ng-bind-html?
Where res2 is the StaticPage content. And res is a json file. Notice that I can get the variables from the scope (also print the json Array) but not possible to iterate with the ng-repeat.
Home Controller:
$rootScope.usefulContent = res;
$rootScope.staticContent = $interpolate(res2)($rootScope);
View:
<div ng-controller="homeCtrl" class="headerSize" id="staticPage">
<div ng-bind-html="staticContent"></div>
</div>
Static Page:
<div class="container content-home">
<div class="row">
<div class="col-sm-6">
<div class="row">
<div class="col-xs-12">
<div ng-repeat="(key, value) in usefulContent | groupBy: 'Order'">
{{key}}
</div>
<ul class="list-group" data-ng-repeat="(key, value) in usefulContent | groupBy: 'Order'">
<li class="list-group-item">{{key}}
<ul class="list-group">
<li class="list-group-item child" data-ng-repeat="link in value">
{{ link.SUBSECTION }}
</li>
</ul>
</li>
</ul>
</div>
</div>
</div>
<div class="col-sm-6">
Col 2
</div>
</div>
</div>
JSON:
[
{
"Order": 1,
"SECTION": "HR",
"SUBSECTION": "Administration",
},
{
"Order": 1,
"SECTION": "HR",
"SUBSECTION": "Self Service",
}
]
Thank you.
Angular doesn't compile the html you included automatically.
This post explains how to do it.

AngularJS - Animating ng-repeat enter one by one

I use angular and angular-animate to animate enter of ng-repeats.
Thats my html
<div class="container" ng-controller="chatController">
<ul ng-repeat="module in CurrentModules" class="timeline">
<li ng-repeat="message in module.ServerMessages" class="timeline-inverted" animate-on-load>
<div class="timeline-panel">
<div class="timeline-body">
<p>{{message}}</p>
</div>
</div>
</li>
</ul>
<div align="center" ng-repeat="answer in module.Answers" animate-on-load>
<div ng-switch on="answer.IsButton">
<div ng-switch-when="true" align="center">
<button ng-click="Answer($event, answer.Value)" class="send-btn">{{answer.Text}}</button>
</div>
<div ng-switch-when="false">
<form>
<input ng-model="answer.Text">
</form>
</div>
</div>
</ul>
</div>
</div>
Note that animate-on-load it's only a directive which allow to stagger animate ng-repeate on page load.
My question - Is it possible to run second ng-repeat after first ng-repeat(and also animations for it) are ended?

Manage complex select/deselect artis/album/song

First of all sorry for the vague title, but i didn’t know how to explain my problem in a better way.
Anyway this is what i want. Let’s say we have three tabs: one showing artist list, one showing album list and one showing songs. All this three tabs has selectable lists and i would like to be able, for example, to select and artist, then going to albums (that now should be show all as selected because i checked the artis) and be able to deselect one album and then, for, example, be able to go to songs and manage the songs individually.
Of course then i should be able to retrive the list of all the songs selected by the user.
EDIT 1
added two images to explain better
EDIT 2
Added some code. At the moment i have only HTML and CSS, i'm waiting for your help for the JS code :)
that's the HTML structure
<div id="tabs" class="page-content tabs overflowTab">
<div id="tab1" class="tab active">
<div class="content-block-title" id="indexScrollOffset">Seleziona artisti</div>
<div class="list-block media-list">
<ul>
<li>
<div class="item-content.modificato">
<div class="item-media modificato">
<i class="icon icon-form-checkbox modificato"></i>
</div>
<a href="#" class="item-link" id="apriTitoliLibro1">
<div class="item-inner modificato">
<div class="item-title-row">
<div class="item-title">Artist 1</div>
</div>
<div class="item-subtitle">Some Text</div>
</div>
</a>
</div>
</li>
<li>
<div class="item-content.modificato">
<div class="item-media modificato">
<i class="icon icon-form-checkbox modificato"></i>
</div>
<a href="#" class="item-link" id="apriTitoliLibro2">
<div class="item-inner modificato">
<div class="item-title-row">
<div class="item-title">Artist 2</div>
</div>
<div class="item-subtitle">Some Text</div>
</div>
</a>
</div>
</li>
[...]
</ul>
</div>
</div>
<div id="tab2" class="tab">
<div class="content-block-title">Seleziona Album</div>
<div class="list-block media list">
<div class="list-group">
<ul>
<li class="list-group-title">Artist 1</li>
<li id="titoliLibro1">
<div class="item-content-modificato titoli">
<div class="item-media modificato fake-media-list">
<i class="icon icon-form-checkbox modificato"></i>
</div>
<a href="personalizzaArticoli.html" class="item-link">
<div class="item-inner modificato titoli">
<div class="item-title-row modificato">
<div class="item-title modificato">Album 1</div>
<div class="item-after modificato"><span class="font-size-artt-label">Some text</div>
</div>
</div>
</a>
</div>
</li>
<li>
<div class="item-content-modificato titoli">
<div class="item-media modificato fake-media-list">
<i class="icon icon-form-checkbox modificato"></i>
</div>
<a href="personalizzaArticoli.html" class="item-link">
<div class="item-inner modificato titoli">
<div class="item-title-row modificato">
<div class="item-title modificato">Album 2</div>
<div class="item-after modificato"><span class="font-size-artt-label">Some text</div>
</div>
</div>
</a>
</div>
</li>
[...]
</ul>
</div>
</div>
</div>
<div id="tab3" class="tab">
<div class="content-block-title">Seleziona song</div>
<div class="list-block searchbar-not-found">
<ul>
<li class="item-content">
<div class="item-inner">
CONTENT HERE IS ADDED DYNAMICALLY FROM A WEBSQL DB
</div>
</li>
</ul>
</div>
</div>
Edit 3
It's a phonegap application and yes, already using jQuery (as less as possibile for performance) :)
Now my question: which is the best way to handle this? My only idea is to create an array and update it with all the elements selected and, in order to show an element as selected or not, checking it with indexOf to see if it exist… is this a good way? I’m a bit worried about performance.
Thanks

AngularJS data binding checkboxes to object if checked

I have a JSON object which I am repeating with ng-repeat, the keys are strings, and the values are an array. I am listing each of the values as a checkbox. I would like to create a second object which contains a list of only the checkboxes that are checked. I want to preserve the structure of the object with keys and values.
I'm unsure how to bind this to a model properly so that the structure is preserved.
http://jsfiddle.net/NDFc2/3/
This is my HTML
<h3 >Dynamic data binding in AngularJS</h3>
<div ng-app ng-controller="Controller" class="container">
<h4>Inputs</h4>
<ul ng-repeat="(parent, values) in inputs">
<span>{{parent}} : </span>
<li ng-repeat="value in values"><label>{{value}}
<input type="checkbox" ng-model="output[parent]" ng-checked="output[parent]" value="value" >
</input></label>
</li>
</ul>
<h4>Outputs</h4>
<ul ng-repeat="(key,value) in inputs">
<li>
{{key}} : {{output[key]}}
</li>
</ul>
</div>
and my JS
function Controller($scope) {
$scope.output = {};
$scope.inputs = {'category': ['one','two','three'], 'color':['blue','green']};
}
Is there some simple way to do this? I have the feeling that I'm missing something minor and this will all work nicely.
My examples have your angular logic in the recommended syntax (non-global). There were also several issues with your markup that I have corrected.
In this example, ng-model="x" is a placeholder that I don't use, but ng-model must be present or an error is thrown. I am using ng-change to handle the link between the checkboxes and $scope.outputs.
Live demo here (click).
Markup:
<div ng-app="myApp" ng-controller="myCtrl">
<h3 >Dynamic data binding AngularJS</h3>
<h4>Inputs</h4>
<ul>
<li ng-repeat="(typeKey, typeVal) in inputs">
<span>{{typeKey}} : </span>
<ul>
<li ng-repeat="value in typeVal">
<label>{{value}}
<input
type="checkbox"
ng-model="x"
ng-change="setOutput(typeKey, $index, value)"
>
</label>
</li>
</ul>
</li>
</ul>
<h4>Outputs</h4>
<ul ng-repeat="(key,value) in inputs">
<li>{{key}} : {{outputs[key]}}</li>
</ul>
</div>
JavaScript:
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.outputs = {};
$scope.inputs = {
'category': ['one','two','three'],
'color':['blue','green']
};
$scope.setOutput = function(typeKey, $index, value) {
$scope.outputs[typeKey] = $scope.outputs[typeKey] || [];
$scope.outputs[typeKey][$index] = value;
};
});
Another Solution
Live demo here (click).
First, I used ng-init to dynamically add the first-level properties from inputs to outputs. Then you just needed to set your ng-model and ng-checked properties to the correct location in outputs.
Markup:
<div ng-app="myApp" ng-controller="myCtrl">
<h3 >Dynamic data binding AngularJS</h3>
<h4>Inputs</h4>
<ul>
<li
ng-repeat="(typeKey, typeVal) in inputs"
ng-init="outputs[typeKey] = outputs[typeKey] || {}">
<span>{{typeKey}} : </span>
<ul>
<li ng-repeat="value in typeVal">
<label>{{value}}
<input
type="checkbox"
ng-model="outputs[typeKey][value]"
ng-checked="outputs[typeKey][value]"
value="outputs[typeKey][value]"
>
</label>
</li>
</ul>
</li>
</ul>
<h4>Outputs</h4>
<ul ng-repeat="(key,value) in inputs">
<li>{{key}} : {{outputs[key]}}</li>
</ul>
</div>
JavaScript:
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.outputs = {};
$scope.inputs = {
'category': ['one','two','three'],
'color':['blue','green']
};
});
You need to bind to the value for the parent, as checkboxes don't work like that. Here's an example:
<h3 >Dynamic data binding in AngularJS</h3>
<div ng-app ng-controller="Controller" class="container">
<h4>Inputs</h4>
<ul ng-repeat="(parent, values) in inputs">
<span>{{parent}} : </span>
<li ng-repeat="value in values"><label>{{value}}
<input type="checkbox" ng-model="output[parent][value]" ng+checked="output[parent][value]" value="value" >
</input></label>
</li>
</ul>
<h4>Outputs</h4>
<ul ng-repeat="(key,value) in inputs">
<li>
{{key}} : {{output[key]}}
</li>
</ul>
</div>
And in the controller create the keys beforehand
function Controller($scope) {
$scope.output = { 'category': {}, color: {} };
$scope.inputs = {'category': ['one','two','three'], 'color':['blue','green']};
}
jsfiddle: http://jsfiddle.net/5eeVc/
Have a look at this plunk, i have handled two different scenarios. Just sharing it as a knowledge article
Plunk
<h3>First Scenario <small>Handling JSON source </small></h3>
<div class="container-fluid">
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6 col-lg-6">
<h4>Available options</h4>
<div ng-repeat="item in itemList">
<mark>{{item.name}}</mark>
<input type="checkbox" ng-model="modelContainer[$index].checked" />
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-6 col-lg-6">
<h4 class="text-success">Selected options</h4>
<ul>
<li ng-repeat="item in modelContainer | filter: {checked:true}">
{{item.item.name}} X
</li>
</ul>
</div>
</div>
</div>
<br>
<p class="text-danger">itemList</p>
<code>{{itemList}}</code>
<br>
<br>
<p class="text-danger">modelContainer</p>
<code>{{modelContainer}}</code>
<hr>
<h3>Second Scenario <small>Handling map Source </small></h3>
<div class="container-fluid">
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6 col-lg-6">
<h4>Available options</h4>
<div ng-repeat="item in Maplist">
<mark>{{item}}</mark>
<input type="checkbox" ng-model="modelContainer1[$index].checked" />
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-6 col-lg-6">
<h4 class="text-success">Selected options</h4>
<ul>
<li ng-repeat="item in modelContainer1 | filter: {checked:true}">
{{item.name}} X
</li>`enter code here`
</ul>
</div>
</div>
</div>

Javascript For Loop HTML syntax with JSON

I'm pretty new to Javascript so I'm not sure if I'm doing this right. First off I'm using JSON to create an array object 'providerlisting'. Next I am creating a for loop and it should loop through the html until there are no more listings in the JSON array. I'm not sure that I did the syntax correctly. I'm also kind of new to asking these questions so I'm sorry in advance if I'm doing this incorrectly.
for (var i=0;i<providerlisting.length;i++)
{ document.write('<div class="entry panel row">
<div class="large-4 columns first">
<div class="name">'providerlisting.nametitle[i]'</div>
<div class="specialty">'providerlisting.caretype[i]'</div>
<div class="peferred">'providerlisting.preferredprovider[i]'</div>
</div>
<div class="large-3 columns second">
<div class="address">'providerlisting.address1[i]'<br />
'providerlisting.address2[i]'<br />
'providerlisting.citystatezip[i]'
</div>
</div>
<div class="large-3 columns third">
<img src="'providerlisting.coverage[i]'" alt="example">
</div>
<div class="large-2 columns fourth">
<div class="status">'providerlisting.status[i]'</div>
<a data-dropdown="actionsMenu2" class="actions button small secondary round dropdown" href="#">Actions</a><br>
<ul id="actionsMenu2" data-dropdown-content="" class="f-dropdown">
<li>Generate PSN</li>
<li>Download Contact</li>
<li>Save to Provider List</li>
<li>View Healthgrades Rating</li>
</ul>
</div>
</div>
');
I can't tell exactly because you haven't posted your JSON, but this should do it:
for(var i = 0; i < providerlisting.length; i++) {
document.write('<div class="entry panel row">
<div class="large-4 columns first">
<div class="name">'+ providerlisting[i].nametitle +'</div>
<div class="specialty">'+ providerlisting[i].caretype +'</div>
<div class="peferred">'+ providerlisting[i].preferredprovider +'</div>
</div>
<div class="large-3 columns second">
<div class="address">'+ providerlisting[i].address1 +'<br />
'+ providerlisting[i].address2 +'<br />
'+ providerlisting[i].citystatezip +'
</div>
</div>
<div class="large-3 columns third">
<img src="'+ providerlisting[i].coverage +'" alt="example">
</div>
<div class="large-2 columns fourth">
<div class="status">'+ providerlisting[i].status +'</div>
<a data-dropdown="actionsMenu2" class="actions button small secondary round dropdown" href="#">Actions</a><br>
<ul id="actionsMenu2" data-dropdown-content="" class="f-dropdown">
<li>Generate PSN</li>
<li>Download Contact</li>
<li>Save to Provider List</li>
<li>View Healthgrades Rating</li>
</ul>
</div>
</div>');
}
I changed every providerlisting.prop[i] into providerlisting[i].prop.
You were also stringing together in the wrong way:
var str = 'some words'someVariable'more words' won't work.
var str = 'some words'+ someVariable +'more words' will.

Categories