Accessing a nested ng-repeat variable - angularjs

This is what I have so far:
<div ng-repeat="storeSection in main.foodBySection" ng-show="main.sortBy === 'storeSection'">
<h4>{{ storeSection.category }}</h4>
<div ng-repeat="ingredient in main.ingredients | orderBy: 'name'"
ng-if="ingredient.storeSection === {{ storeSection.category }}">
<p><input type="checkbox" ng-model="ingredient.added"/> {{ ingredient.name }} <span
class="ingredient-source">- {{ ingredient.source }}</span></p>
</div>
</div>
Basically, I only want to display the items in main.ingredients that have the same category as the header, but I cannot access {{ storeSection.category }} once I use a new ng-repeat. Any ideas?

<div ng-repeat="storeSection in main.foodBySection" ng-show="main.sortBy === 'storeSection'">
<h4>{{ storeSection.category }}</h4>
<div ng-repeat="ingredient in main.ingredients | orderBy: 'name'"
ng-if="ingredient.storeSection === storeSection.category">
<p><input type="checkbox" ng-model="ingredient.added"/> {{ ingredient.name }} <span
class="ingredient-source">- {{ ingredient.source }}</span></p>
</div>
</div>

<div ng-repeat="storeSection in main.foodBySection" ng-show="main.sortBy === 'storeSection'">
<h4>{{ storeSection.category }}</h4>
<div ng-repeat="ingredient in main.ingredients | orderBy: 'name'"
ng-if="ingredient.storeSection === storeSection.category ">
<p><input type="checkbox" ng-model="ingredient.added"/> {{ ingredient.name }} <span
class="ingredient-source">- {{ ingredient.source }}</span></p>
</div>
</div>
Don't interpolate storeSection.category, ng-if works with angular-expressions just like the left-hand operandingredient.storeSection is getting evaluated by it

As pointed out in another answers, the cause of the problem is that you're using interpolation in your ngIf directive, which one works with angular-expressions.
Tips:
Use preferably ngIf instead of ngShow(in your first <div>).
I also recommend you to use the native filter instead of this check using ngIf(in ngRepeat) and to use the ngRepeat#special-repeats.
So you can have something like this:
<div ng-repeat-start="storeSection in main.foodBySection" ng-if="main.sortBy === 'storeSection'">
<h4 ng-bind="storeSection.category"></h4>
</div>
<div ng-repeat-end ng-repeat="ingredient in main.ingredients | filter: { storeSection: storeSection.category } | orderBy: 'name'">
<p>
<input type="checkbox" ng-model="ingredient.added" /> {{ ingredient.name }} <span class="ingredient-source">- {{ ingredient.source }}</span></p>
</div>

Related

AngularJs Filter hide options if no products

I have found this filter and I'm trying to edit it so that you select an option then it greys out the other option For example if you select (vegetable) you can only select (Orange).
I'm not sure how to out it into code but I the logic for it. On the dynamicFilter function need to update the filter options count and if its less then 1 needs to add a class or something.
Here is my code pen any help would be awesome.
https://codepen.io/natedog213/pen/mwPxmy
<div ng-app='angularDemo' ng-cloak>
<md-toolbar layout-padding>
<h2>Filtering with Angular Filters</h2>
</md-toolbar>
<div layout="row" ng-controller="angularController as ctrl">
<div flex="20" class="sidebar" layout-padding>
<!-- Repeat the filter types and there options -->
<div ng-repeat="filter in Filters">
<h3>{{filter.name}}</h3>
<ul class="filteroptions">
<li ng-repeat="option in filter.options">
<input id="{{option.value}}" class="filter_option" type="checkbox" data-name="{{option.value}}" ng-model="option.IsIncluded" ng-checked="option.IsIncluded">
<label for="{{option.value}}">{{option.value}}</label>
</li>
</ul>
</div>
</div>
<div flex="50" layout="row">
<div flex ng-repeat="product in products | dynamicFilter:Filters:this" class="product">
{{product.name}}
</div>
</div>
</div>
</div>
You can add condition according to your filter like as ng-show="(products | unique:'price').length > 0"
Try this
<div class="side_menu">
<h2>Product Types</h2>
<div data-ng-repeat="product in products | unique:'type'">
<input id="{{product.type}}" type="checkbox" ng-model="search.type" ng-true-value="'{{product.type}}'" ng-false-value='' /><label for="{{ product.type }}">{{ product.type }} </label>
</div>
<br>
<h2 ng-show="(products | unique:'price').length > 0">Product Price</h2>
<div data-ng-repeat="product in products | unique:'price'">
<input id="{{product.price}}" type="checkbox" ng-model="search.type1" ng-true-value="'{{product.price}}'" ng-false-value='' /><label for="{{ product.price }}">{{ product.price }} </label>
</div>
</div>

Angular 1.5 compare ng-repeat $index with number

I'm trying to do something very simple in Angular but can't seem to get it working. I want to use an ng-repeat and compare the $index with a number. somehow there is an error in my code, but i cannot find it. Any help will be uch appreciated. Cheers!
<li ng-repeat="(key, structure) in structures">
{{ $index }}
<div class="row" ngIf="{{$index}} == 1">
<div class="col8 itemBox"></div>
<div class="col4-odd-fix itemBox"></div>
</div>
<input style="display: inline-block" name="structure" type="radio" value="{{key}}" ng-model="$parent.structure" required>
{{key}}
</li>
This should work:
<li ng-repeat="(key, structure) in structures">
{{ $index }}
<div class="row" ng-if="$index === 1">
<div class="col8 itemBox"></div>
<div class="col4-odd-fix itemBox"></div>
</div>
<input style="display: inline-block" name="structure" type="radio" value="{{key}}" ng-model="$parent.structure" required> {{key}}
</li>
You had a couple of issues:
You need to use ng-if, ngIf is Angular 2 syntax.
You shouldn't use {{ }} inside ng-if, you just need $index === 1.

Order JSON data in Angular (Ionic)

I need help with ordering JSON in ng-repeat. I want to order my data by item.id (from highest to lowest). I tried many tips that are over internet but maybe I'm just doing something wrong because none of them are working for me... My code:
$http.get('http://jastrzebieonline.pl/drogi/data/najnowsze.php')
.success(function(res){
$scope.news = res;
console.log("Ok");
})
.error(function(data, status) {
console.log("Error");
})
<div ng-repeat="item in news">
<a href="{{ item.id }}">
<img src="{{ item.image }}">
<h2>{{ item.title }}</h2>
<p>{{ item.date }}</p>
<p>{{ item.text }}</p>
</div>
</a>
</div>
You can use the orderBy filter:
<div ng-repeat="item in news | orderBy:'id'">
<a href="{{ item.id }}">
<img src="{{ item.image }}">
<h2>{{ item.title }}</h2>
<p>{{ item.date }}</p>
<p>{{ item.text }}</p>
</div>
</a>
</div>
You can use the "orderBy" filter to do the job.
By the way, you should use 'ngHref' and 'ngSrc' directives instead of the original 'href' and 'src' attribute.
<div ng-repeat="item in news | orderBy:'id':true">
<a ng-href="{{ item.id }}">
<img ng-src="{{ item.image }}">
<h2>{{ item.title }}</h2>
<p>{{ item.date }}</p>
<p>{{ item.text }}</p>
</div>
</a>
</div>
references:
https://docs.angularjs.org/api/ng/filter/orderBy
https://docs.angularjs.org/guide/filter

Angular 2 ngFor not rendering

I'm trying to use ngFor under a template and retrieve children to modify them later.
Here is my template:
<div (mouseenter)="showTooltip()" (mouseleave)="hideTooltip()" (mousedown)='onMouseDown($event)' class='slider slider-{{ orientation }}'>
<div class='slider-track'>
<div #trackLow class='slider-track-low' [ngClass]="{hide: (type === 'slider' || selection === 'none' || selection === 'after')}"></div>
<div #trackSelection class='slider-selection' [ngClass]="{hide: (selection === 'none')}"></div>
<div #trackHigh class='slider-track-high' [ngClass]="{hide: (selection === 'none' || selection === 'before')}"></div>
</div>
<div class="slider-tick-label-container" [ngClass]="{hide: labels.length === 0}">
<div #labelElements *ngFor="let label of ticksLabels" class="slider-tick-label">{{ label }}</div>
</div>
<div class="slider-tick-container" [ngClass]="{hide: ticks.length === 0}">
<div #tickElements *ngFor="let tick of ticks" class="slider-tick {{ handleType }}"> </div>
</div>
<div #tooltipMain class="tooltip tooltip-main {{ tooltipPosition }}" role="presentation">
<div class="tooltip-arrow"></div>
<div #tooltipMainInner class="tooltip-inner">Current value: {{ value }}</div>
</div>
<div #tooltipMin class="tooltip tooltip-min {{ tooltipPosition }}" role="presentation">
<div class="tooltip-arrow"></div>
<div #tooltipMinInner class="tooltip-inner"></div>
</div>
<div #tooltipMax class="tooltip tooltip-max {{ tooltipPosition }}" role="presentation">
<div class="tooltip-arrow"></div>
<div #tooltipMaxInner class="tooltip-inner"></div>
</div>
<div #minHandle tabindex="0" (keydown)="keydown(0, $event)" (focus)="showTooltip()" (blur)="hideTooltip()" class='slider-handle min-slider-handle {{ handleType }}' role='slider'></div>
<div #maxHandle tabindex="0" (keydown)="keydown(1, $event)" (focus)="showTooltip()" (blur)="hideTooltip()" class='slider-handle max-slider-handle {{ handleType }}' [ngClass]="{hide: type === 'slider'}" role='slider'></div>
</div>
Both ngFor are rended like this in DOM:
<div class="slider-tick-container">
<!--template bindings={}-->
</div>
In my component I try to retrieve elements with:
#ViewChildren('tickElements') private tickElements: QueryList<ElementRef>;
#ViewChildren('labelElements') private labelElements: QueryList<ElementRef>;
Both are undefined because of the rendering problem...
I try to remove the template and replace it by <div *ngFor="let i of [1,2,3,4]">{{i}}</div> and same problem !
The full code is available here: https://github.com/jaumard/ng2-bootstrap/blob/ticksLabels/components/slider/slider.component.ts#L15

AngularJS loop show the same image

In my project I need to show for each element its image.
This is my html:
<div ng-repeat="event in events.events" class="event show-place" data-id="#{{ event.id }}"
ng-mouseenter="selectEvent(event)">
<div class="cover-events">
<img width="100%" height="100%"
ng-src="#{{ selectedEvent.place.image | addFullPathToImage }}"
alt="#{{ selectedEvent.place.name }}"
>
<div class="user-info">
<a href="#{{ event.created_by.profileSlug }}">
<immagine-profilo hashid="#{{ event.created_by.hashid }}"
username="#{{ event.created_by.profile.username }}"
photo="#{{ event.created_by.profile.photo }}"></immagine-profilo>
<div class="user-event" ng-bind="event.created_by.profile.username"></div>
<rating class="user-experience" stars="#{{ event.created_by.level }}"
icon="star"></rating>
</a>
</div>
</div>
<div class="info-event">
<div vertilize class="content-info">
<div class="title-place">
<h2 ng-bind="event.place.name"></h2>
</div>
<div class="date-time">
#{{ event.unix_date | formatDate }} | #{{ event.time_start }}
</div>
<div class="event-feature">
<div class="category pull-left">
<img ng-src="#{{ event.category.icon | addFullPathToIcon }}"
alt="#{{ event.category.name }}">
<span ng-bind="event.category.name"></span>
</div>
<div class="difficulty pull-left">
<rating class="difficulty-bar" stars="#{{ event.difficulty_level }}"
icon="circle"></rating>
<span>
{{ trans('frontend/events/events.strings.difficulty') }}
</span>
</div>
<div class="participants pull-left">
<div class="speech-bubble speech-bubble-bottom">
#{{ event.participants_count }}
</div>
<span>{{ trans('frontend/events/events.strings.participants') }}</span>
</div>
</div>
</div>
<a class="link-with-arrow"
href="{{ localeRoute('events.event', [null, null])}}/#{{ event.slug }}/#{{ event.id }}">
{{ trans('frontend/pages/place.sidebar.links.read_more') }}
</a>
</div>
the image code is this:
<img width="100%" height="100%"
ng-src="#{{ selectedEvent.place.image | addFullPathToImage }}"
alt="#{{ selectedEvent.place.name }}">
and this is the filter:
app.filter('addFullPathToIcon', function () {
return function (text) {
return window.paths.categoriesPath + "/" + text;
}
});
When I run the code, the same image is shown for each event, if I click on one of it, the images will change and show the second image for every event.
Someone can help me to see the error?
It looks to me like there's only one "selectedEvent", and it is defined on ng-mouseenter. Perhaps you want
<img width="100%" height="100%"
ng-src="#{{ event.place.image | addFullPathToImage }}"
alt="#{{ event.place.name }}">
instead inside your ng-repeat if you don't want all the images to change with the event that is selected...

Resources