Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular directive with ng-repeat, ng-show "Show more" and lazy-load

I use this directive, iterating over an array "myArr", filtering for a few conditions.

<myDirective 
    myData='item' 
    ng-repeat="item in filteredArr = (myArr | filter:searchField | filter:checkboxFilter)" 
    ng-show="$index < visible" 
/>

This gives me two issues I'd like to get some input on:

a) the ng-show part is there because I have a condition that handles this:

<p>
    <a ng-click='visible=visible+10' ng-show='visible < filteredArr.length'>Show more</a>
</p>

in order to show or hide the "Show more" part. I cannot come up with another idea on toggling this and/or the items itself. $scope.visible is, inside the controller, set to 10, once we start. I couldn't use limitTo as it does not give me the possibility to determine if there's more to show or not, as it of course "chops" the array to the set limit.

b) Inside the directive, the template prints an

<img ng-src="..."> 

tag. How can I prevent these images to load as long as they're not shown in the above structure?

Thanks a lot in advance!

like image 527
chrney_p Avatar asked Dec 12 '14 06:12

chrney_p


People also ask

Can you use Ng if and Ng-show together?

ng-if is better in this regard. Using it in place of ng-show will prevent the heavy content from being rendered in the first place if the expression is false. However, its strength is also its weakness, because if the user hides the chart and then shows it again, the content is rendered from scratch each time.

How do I get the index of an element in NG-repeat?

Note: The $index variable is used to get the Index of the Row created by ng-repeat directive. Each row of the HTML Table consists of a Button which has been assigned ng-click directive. The $index variable is passed as parameter to the GetRowIndex function.

How do I use track in NG-repeat?

To avoid this problem, you can use "track by" with ng-repeat. In track by you have to use angular expression that will be used to uniquely identify the each items in collection or model. "track by" tells the angular js that how angular js will track the association between DOM and the model (i.e. collection).

What is ng-repeat directive in AngularJS?

Angular-JS ng-repeat Directive Last Updated : 09 Aug, 2019 Angular-JS ng-repeat directive is a handy tool to repeat a set of HTML code for a number of times or once per item in a collection of items. ng-repeat is mostly used on arrays and objects.

What is ng-show directive in angular?

The ng-show directive shows the specified HTML element if the expression evaluates to true, otherwise the HTML element is hidden.

What is the use of ng options in angular?

AngularJS ng-options Directive 1 Definition and Usage. The ng-options directive fills a <select> element with <options>. The ng-options directive uses an... 2 Syntax. Supported by the <select> element. 3 Parameter Values. An expression that selects the specified parts of an array to fill the select element. More ...

How do I display repeating values in an angular controller?

Angular provides a directive called "ng-repeat" which can be used to display repeating values defined in our controller. Let's look at an example of how we can achieve this.


1 Answers

Use ng-if instead of ng-show.

Unlike ng-show, falsey ng-if removes the element from the DOM.

EDIT:

Also, you can, in fact, use limitTo filter, which would make your code much cleaner:

<div ng-init="limit = 2">
  <foo ng-repeat="item in items | limitTo: limit as results"></foo>
</div>
<button ng-hide="results.length === items.length" 
        ng-click="limit = limit +2">show more...</button>

plunker

like image 109
New Dev Avatar answered Oct 01 '22 20:10

New Dev