1

I created 'show more' link for dynamic list items which have more than three items but I have a problem with change this link to 'show less' after click 'show more'. I need two another icon for that. Any ideas how I can change it? It is possible to create it only with ng directives in HTML without functions in a controller?

My code:

<ul ng-if="::document.actUnitMonographViewList" class="commentary-snippets">
  <li ng-repeat="monograph in document.actUnitMonographViewList | limitTo:showMore ? document.actUnitMonographViewList.length : 3">
     <i ng-if="document.actUnitMonographViewList.length > 1"
        class="fa fa-chevron-right"></i>
      <a href="" ng-href="{{ ::getPublicationLink(monograph, 'monograph') }}"
         ng-class="{'arrow-link': document.actUnitMonographViewList.length > 1}" 
         user-preferences-target>{{::monograph.title}}
      </a>
   </li>
   <li ng-show="document.actUnitMonographViewList.length > 3">
      <i class="fa fa-chevron-down"></i>
      <a ng-click="showMore = true">Show more</a>
   </li>
   <li ng-show="document.actUnitMonographViewList.length > 3">
      <i class="fa fa-chevron-up"></i>
      <a ng-click="">Show less</a>
   </li>
</ul>
  • which angular version do you use? – J. Knabenschuh Dec 13 '18 at 13:59
  • 1
    Have a look at:https://stackoverflow.com/questions/32211888/how-to-load-more-elements-with-angular – Prashant Pimpale Dec 13 '18 at 14:01
  • This appears to be a data hiding problem. New AngularJS developers often do not realize that `ng-repeat`, `ng-switch`, `ng-view`, `ng-include` and `ng-if` all create new child scopes, so the problem often shows up when these directives are involved. See [What are the nuances of scope prototypal / prototypical inheritance in AngularJS?](https://stackoverflow.com/questions/14049480/what-are-the-nuances-of-scope-prototypal-prototypical-inheritance-in-angularjs). The solution is to use a dot, i.e. `ng-click="document.showMore = true"`. – georgeawg Dec 13 '18 at 14:23
  • There is also potential for problems when using [`document`](https://developer.mozilla.org/en-US/docs/Web/API/Window/document) in an AngularJS expression. `document` is a global variable and may cause problems when using it as a scope variable. – georgeawg Dec 13 '18 at 14:29

3 Answers3

1

I changed my code like below and it works! :)

<ul ng-if="::document.actUnitMonographViewList" class="commentary-snippets">
  <li ng-repeat="monograph in document.actUnitMonographViewList | limitTo: monograph.limit ? document.actUnitMonographViewList.length : 3">
  <i ng-if="document.actUnitMonographViewList.length > 1" class="fa fa-chevron-right"></i>
    <a href="" ng-href="{{ ::getPublicationLink(monograph, 'monograph') }}" ng-class="{'arrow-link': document.actUnitMonographViewList.length > 1}" user-preferences-target>
      {{::monograph.title}}
    </a>
  </li>
  <li ng-show="document.actUnitMonographViewList.length > 3">
    <i class="list-items-toggle-arrow" ng-class="{'fa fa-chevron-up': monograph.limit, 'fa fa-chevron-down': !monograph.limit}" ng-click="monograph.limit = !monograph.limit"></i>
    <a ng-click="monograph.limit = !monograph.limit" class="list-items-toggle-link">{{monograph.limit ? 'LIST_ITEMS_CONTENT.LESS' : 'LIST_ITEMS_CONTENT.MORE' | translate}}</a>
  </li>
</ul>
Prashant Pimpale
  • 10,349
  • 9
  • 44
  • 84
0

You can use an ngIf, see following:

<ul ng-if="::document.actUnitMonographViewList" class="commentary-snippets">
  <li ng-repeat="monograph in document.actUnitMonographViewList | limitTo:showMore ? document.actUnitMonographViewList.length : 3">
     <i ng-if="document.actUnitMonographViewList.length > 1" class="fa fa-chevron-right"></i>
      <a href="" ng-href="{{ ::getPublicationLink(monograph, 'monograph') }}" ng-class="{'arrow-link': document.actUnitMonographViewList.length > 1}" user-preferences-target>{{::monograph.title}}</a>
   </li>
   <li ng-show="document.actUnitMonographViewList.length > document.itemsLimit">
      <i class="fa fa-chevron-down"></i>
      <a ng-if="showMore==false" ng-click="showMore = true" class="">Show more</a>
      <a ng-if="showMore==true" ng-click="showMore = false" class="">Show less</a>
   </li>
</ul>
J. Knabenschuh
  • 747
  • 4
  • 15
  • I made sth like that and it doesn't work... I need another icon which I want to use for 'show less' link so it must be in separate
  • tag
  • – Natalia Gurgul Dec 13 '18 at 14:27