how to bind 3 arrays simultaniously using angularjs - angularjs

I tried using ng-repeat value,group1,group2
i'm getting
grp1
grp2
abc
def
value1
value2
I need
grp1
abc
valu1
grp2
def
valu1
Html:
<ul>
<li ng-repeat="heading in group1"> {{heading}} </li> // heading
<li ng-repeat="subheading in group2"> {{subheading }} </li> // sub heading
<li ng-repeat="val in value"> {{val}} </li> // value
</ul>
JavaScript:
$scope.group1 = [grp1,grp2];
$scope.group2 = [abc,def];
$scope.value = [value1,value2];

use $index to get other sub values
<div ng-repeat="eachval in group1">
<p>{{ eachval }}</p>
<p style="margin-left:15px">{{ group2[$index]}}</p>
<p style="margin-left:25px">{{ value[$index]}}</p>
</div>
Here is the WORKING FIDDLE

You should use a single array with three different keys
$scope.arrayList = [{group1Name : 'grp1',group2Name: 'abc', valueName: 'val1'},
{group1Name : 'grp2',group2Name: 'def', valueName: 'val2'}]
htlm would look like this
<li ng-repeat="group in arrayList">
<div>{{group.group1Name}}</div>
<div>{{group.group2Name}}</div>
<div>{{group.valueName}}</div>
</li>
You can write a simple javascript to modify your 3 arrays in 1 array.
Hope this helps

Use ng-repeat start and end points to iterate the first array, then use $index to reference the other elements:
<ul>
<li ng-repeat-start="heading in group1">{{heading}}</li> // heading
<li> {{group2[$index]}} </li> // sub heading
<li ng-repeat-end> {{val[$index]}} </li> // value
</ul>

Related

Avoid multiple use of a Filter in AngularJs

I am trying to avoid using the same filter on the same collection multiple times in AngularJS application.
What is the correct way to do it:
<div ng-app="myApp" ng-controller="PeopleCtrl">
<ul ng-init="(person in people|filter:{show:true}) as myPeople" ng-if="myPeople.length>0">
<h4>My People</h4>
<li ng-repeat="person in myPeople">{{person.name}}; display: {{person.show}};
</ul>
</div>
CODE PEN
When I use (person in people|filter:{show:true}) as myPeople I try to use an alias in order to not repeat the same filter{show:true} everywhere...
You should do it something like this:
<div ng-init="myPeople = (people | filter:{show:true})">
<div ng-if="myPeople.length>0">
<h4> My People</h4>
<ul>
<li ng-repeat="person in myPeople">
{{person.name}}; age: {{person.age}}; show: {{person.show}};
</li>
</ul>
</div>
</div>

Angular switch from ng-show to ng-repeat filter / how to

What is the correct syntax to switch from my simple ng-show to a filter in my ng-repeat? (item in list | filter:{item.value == 'abc'} or so)
<ol ng-repeat="item in list">
<li ng-show="item.value == 'abc'">test</li>
</ol>
Do not use ng-repeat inside <ol> tag.
This will repeat <ol> tag each time with single <li>.
Use ng-repeat in <li> tag to repeat <li> tag inside <ol> tag and use filter with ng-repeat.
use like this :
<ol>
<li ng-repeat="item in list | filter: { value : 'abc' }">test</li>
</ol>
working plunkr
<ol ng-repeat="item in list | filter:{value: 'abc'}">
<li>test</li>
</ol>
Plunker: https://plnkr.co/edit/uw0LNygWbj4Njhp1n7PN?p=preview
Try this
<ol ng-repeat="item in list | filter: {value: 'abc'}">
<li>test</li>
</ol>
Angular Filter doc
You can use nf-repeat Filter
<ol ng-repeat="item in list | filter : {value : 'abc'}">
<li>test</li>
</ol>
read AngularJS API Document

trying to use ng-repeat

I have the following data which i want to use ng repeat on:
$scope.doctors=[{patch:'BARIJPUR',doctor:'RAMA SENA',doctor:'SMRITI IRANI',doctor:'JAGDISH NAIR',
patch:'KIRI',doctor:'ASHISH NAIK',doctor:'SMRITI IRANI',doctor:'SAIRAJ NAIK',
patch:'JAMNAGAR',doctor:'RATAN PANDEY',doctor:'RAMAN SHIVOLKAR'
}];
Im trying to use it like this:
<div id='cssmenu' class="visible-xs" >
<ul>
<li class='active'><a href='#'><span>Doctors</span></a></li>
<li class='has-sub' >{{doc.patch}}</span>
<ul>
<li><span>{{doc.doctor}}</span></li>
</ul>
</li>
</ul>
</div>
the current output im getting is shown in the following link:
current output
the expected output im getting is shown in the following link:
expected output
am i doing something wrong? have been at it for 1 and half hour now!
From your comment I assume that you're having a problem with the JS associative array, so here is an example of implementation:
$scope.doctors=[
{patch:'BARIJPUR',doctors:['RAMA SENA' 'SMRITI IRANI', 'JAGDISH NAIR',]},
{patch:'KIRI', doctors:['ASHISH NAIK', 'SMRITI IRANI', 'SAIRAJ NAIK']},
{patch:'JAMNAGAR', doctors:['RATAN PANDEY', 'RAMAN SHIVOLKAR']}
];
<div id='cssmenu' class="visible-xs" >
<ul>
<li class='active'><a href='#'><span>Doctors</span></a></li>
<li class='has-sub' ng-repeat="doc in doctors"><a href='#'><span>{{doc.patch}}</span></a>
<ul>
<li ng-repeat="doctor in doc.doctors"><a href='#'><span>{{doctor}}</span></a></li>
</ul>
</li>
</ul>
</div>
<div class="row" ng-repeat="doc in doctors">{{doc.patch}}
<div class="cell" ng-repeat="cell in doc.doctor">{{cell}}
</div>
</div>
try this
You have two problems:
Array definition
Instead of creating multiple doctor entities you should just create an array of them:
$scope.doctors=
[
{"patch":"BARIJPUR", "doctors":["RAMA SENA", "SMRITI IRANI","JAGDISH NAIR"]},
{"patch":"Anna", "doctors":["ASHISH NAIK", "SMRITI IRANI", "SAIRAJ NAIK"]},
{"patch":"Peter","doctors":["RATAN PANDEY", "RAMAN SHIVOLKAR"]}
];
ng-repeat
You used it in wrong place. It should be applied to <li> element - which basically you want to repeat, not to <a> or <span>:
<div id='cssmenu'>
<ul>
<li class='active'><a href='#'><span>Doctors</span></a></li>
<li ng-repeat="doc in doctors" class='has-sub'><a href='#'><span>{{doc.patch}}</span></a>
<ul>
<li ng-repeat="doc in doc.doctors"><a href='#'><span>{{doc}}</span></a></li>
</ul>
</li>
</ul>
Demo plnkr
<div class="row" ng-repeat="doc in doctors">{{doc.patch}}
<div class="cell" ng-repeat="cell in doc.doctor">{{cell}}
</div>
</div>
yes this works for me! done! solved!

Binding string to ng-repeat item value with matching name

This list sets the value of abc according to the item clicked.
<ul>
<li><a ng-click="abc = 'score'">Score</a></li>
<li><a ng-click="abc = 'count'">Count</a></li>
<li><a ng-click="abc = 'average'">Average</a></li>
</ul>
List.items contains values that correspond to score, count and average. Items are ordered by the value of abc.
<ul>
<li ng-repeat="item in list.items | orderBy:'-' + abc">
{{ 'item.' + abc }}
</li>
</ul>
My only problem is this line: {{ 'item.' + abc }}
How do I bind the value of {{ item.score }}, {{ item.count }} or {{ item.average }} based on the value of abc?
Use [] object notation when object properties are variables. This is standard javascript practice
{{ item[abc] }}

How to iterate through an array of arrays in JSON using ng-repeat?

I am trying to iterate over a JSON object userTypes.
For the below code:
In the 1st ng-repeat:
{{user.type}} outputs 'Parent'(as expected),
{{user.options}} outputs '[{"option1":"11QWERT","option2":"22QWERT"}]'(as expected).
But in the 2nd ng-repeat, I am not able to iterate through the user.options and output each of the {{options}}
What should be changed to get the option1 and option2 as the outputs in 2nd ng-repeat ?
JS snippet
var userTypes = [{
"type": 'Parent',
"options": [{
"option1": "11QWERT",
"option2": "22QWERT"
}]
}]
HTML snippet
<li ng-repeat="user in userTypes">
<p>{{user.type}}</p>
<p>{{user.options}}</p>
<li ng-repeat="option in user.options">
<p>
{{option}}
</p>
</li>
</li>
Replace your child <li> with <ul> and then you can iterate user.options like so:
<li ng-repeat="user in userTypes">
<p>{{user.type}}</p>
<ul ng-repeat="(key, value) in user.options[0]">
<p>{{key}}: {{value}}</p>
</ul>
</li>
Or if your options may include more then one object:
<li ng-repeat="user in userTypes">
<p>{{user.type}}</p>
<ul ng-repeat="option in user.options">
<li ng-repeat="(key, value) in option">{{key}}: {{value}}</li>
</ul>
</li>
If you don't need object keys:
<ul ng-repeat="option in user.options">
<li ng-repeat="item in option">{{item}}</li>
</ul>
Fiddle
Extended explanation:
In your example you have <li> tag inside another <li>:
<li ng-repeat="user in userTypes">
<li ng-repeat="option in user.options">
</li>
</li>
Since it is not a valid HTML browser will interprets this markup to following:
<li ng-repeat="user in userTypes">
</li>
<li ng-repeat="option in user.options">
</li>
Since ng-repeat creates new scope for each iteration you can't access user variable in second ng-repeat and iterator wouldn't run.
For this exact JSON input it should be like this:
<li ng-repeat="option in user.options">
<p>
{{option.option1}}
{{option.option2}}
</p>
</li>
However as you said you want non fixed number of options, update your JSON to be like this:
"options":["11QWERT","22QWERT"]
And then your code should work as you wanted it.
You can add each new element to list with simple coma before it.
Since user.options is an array you should loop it again. By doing that you will get an object, with that object you can access your options1 and option2 easily.
please refer working plunker:
http://embed.plnkr.co/5c3i5fY50jLP0fFgxkUX/preview
see through the code if you have any doubt.
Hope this helps
A little aside, but just found out you can render a valid list HTML using ng-repeat-start/end in combination with ng-if="false" for the given array of arrays to flatten them:
<ul>
<script ng-repeat-start="user in userTypes" ng-if="false"></script>
<li ng-repeat="item in user.options">{{item}}</li>
<script ng-repeat-end="" ng-if="false"></script>
</ul>
Just access the propertie of the option object in the second ng-repeat. Like option.option1
You should probably make your user.options an object and not an array containing a single object.
Note the difference between these:
[{"option1":"11QWERT","option2":"22QWERT"}]
{"option1":"11QWERT","option2":"22QWERT"}
You can then iterate the options with an ng-repeat like discussed here:
<li ng-repeat="(key, value) in user.options">
<p>{{ key }}: {{ value }}</p>
</li>

Resources