1

I'm trying to implement something similar to this solution in angular2. I know I can use the slice pipe to limit the number of iterations (similar to limitTo). The thing is, there's no ng-init directive on angular2, and local variables doesn't seem to work in this scenario, and I can't use a variable in the component's level since it should be local to each sub entity (as seen in the example below).

 <template ngFor #record [ngForOf]="records" #i="index">
  <template #showMoreLocal="2" ngFor #object [ngForOf]="record.objects | slice:0:showMoreLocal" #j="index">
    ....some htmls....
    <button *ngIf="showMoreLocal < record.objects.length" (click)="showMoreLocal = showMoreLocal + 10">..</button>
  </template>
</template>
Community
  • 1
  • 1
natiz
  • 563
  • 1
  • 15
  • 29
  • 1
    what you actually want to do with *ngFor ? – Pardeep Jain Apr 17 '16 at 07:42
  • 1
    `[ngIf]` should be `*ngIf`, for one thing. – drew moore Apr 17 '16 at 08:42
  • What's not working as expected? – Günter Zöchbauer Apr 17 '16 at 12:42
  • I fixed the ngIf, but that's besides the point. I want to show X first values of each record.objects, and provide the ability to show X more when the user clicks on the button 'show more'. An example data structure would be: records = [{objects: [1,2,3,4]}, {objects:[3,4,5,6,7,8,9]}], so for the 2nd record, I want to display 3,4, then when the user clicks, also show 5,6, and so on. – natiz Apr 17 '16 at 16:40
  • @GünterZöchbauer - the local variable is not really being set, so the slice pipe fails. It has to be a local variable (or similar) since the records/objects are subject to change anytime. – natiz Apr 17 '16 at 16:42
  • 1
    Why don't you use ` – Günter Zöchbauer Apr 17 '16 at 18:30
  • There should be a local variable that indicates how many records to show, and each you click on the button it should increment it until you reach record.objects.length – natiz Apr 18 '16 at 12:19
  • See also http://stackoverflow.com/questions/42458664/how-to-show-1-element-in-ngfor-in-angular2/42458755#42458755 – Günter Zöchbauer Feb 28 '17 at 13:26

0 Answers0