BeniaminoBaggins
BeniaminoBaggins

Reputation: 12433

Angular 2 *ngFor display component template not working

I have this collection in "results.component.ts": results: Result[]

I would like to iterate over the collection, displaying each result.

I would like to have a seperate component for this called result.component.ts. This is because a result could get complex and large.

What I have so far is not displaying anything. The output html has some commented out template bindings:

<div _ngcontent-fud-12="" class="col-sm-8 offset-sm-2 col-md-6 offset-md-3 col-xl-4 
            offset-xl-4">
            <!--template bindings={
  "ng-reflect-ng-for-of": "[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]"
}--><!--template bindings={}--><!--template bindings={}--><!--template bindings={}--><!--template bindings={}--><!--template bindings={}--><!--template bindings={}--><!--template bindings={}--><!--template bindings={}--><!--template bindings={}--><!--template bindings={}-->
        </div>

Here is my relevant code:

results.component.html:

<div id="results" class="text-uppercase">
    <div id="up-button-row" class="row">
        <div class="col-sm-8 offset-sm-2 col-md-6 offset-md-3 col-xl-4 
            offset-xl-4">
            <button class="m-x-auto" md-fab [disableRipple]="true" (click)="scrollUp()"></button>
        </div>
    </div>
    <div class="row" *ngIf="noResults">
        <div class="col-sm-8 offset-sm-2 col-md-6 offset-md-3 col-xl-4 
            offset-xl-4">
            <h2 class="m-x-auto">No vegan stuff found :-(</h2>
        </div>
    </div>
        <div class="row" *ngIf="!noResults">
        <div class="col-sm-8 offset-sm-2 col-md-6 offset-md-3 col-xl-4 
            offset-xl-4">
            <div *ngFor="let result of results"><result></result></div>
        </div>
    </div>
</div>

results.component.ts:

import { Component, AfterViewInit } from '@angular/core';
import { ResultComponent } from './result.component';
import { Result } from '../result'

@Component({
   selector: 'results-div',
   templateUrl: 'app/find-page/results.component.html',
   styleUrls: ['app/find-page/results.component.css' ],
   directives:[ResultComponent]
})
export class ResultsComponent implements AfterViewInit {
   results: Result[];
   noResults: boolean;
   ngAfterViewInit() {
this.scrollDown();
   }

   scrollDown() {
      ScrollToAnchor.goToTargetBottom("#results");
   }

   scrollUp() {
     ScrollToAnchor.goToTarget("#find-page");
   }
}

result.component.html:

<div class="col-sm-6 col-lg-2" style="margin-top:20px; padding: 25px;">
   <div class="product-item scale-on-hover" (click)="setCurrentlySelectedProduct()">
      <div [ngStyle]="{background: result.imagePath}" id="image"></div>
      <div id="info">
         <h6 id="brand" class="medium-text">{{brand}}</h6>
         <h6 id="name" class="medium-text">{{name}}</h6>
      </div>
   </div>
</div>

result.component.ts:

import { Component, AfterViewInit } from '@angular/core';
import { Result } from '../result';

@Component({
   selector: 'result',
   templateUrl: 'app/find-page/result.component.html',
   styleUrls: ['app/find-page/result.component.css' ]
})
export class ResultComponent{}

How do I pass the result data for 1 result from results.component to result.component.

How do I get the result.component.html to display once for each result?

Upvotes: 4

Views: 9015

Answers (1)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657338

@Component({
   selector: 'result',
   templateUrl: 'app/find-page/result.component.html',
   styleUrls: ['app/find-page/result.component.css' ]
})
export class ResultComponent{
  @Input() result;
}
<div *ngFor="let result of results"><result [result]="result"></result></div>
<div class="col-sm-6 col-lg-2" style="margin-top:20px; padding: 25px;">
   <div class="product-item scale-on-hover" (click)="setCurrentlySelectedProduct()">
      <div [ngStyle]="{background: result.imagePath}" id="image"></div>
      <div id="info">
         <h6 id="brand" class="medium-text">{{result.brand}}</h6>
         <h6 id="name" class="medium-text">{{result.name}}</h6>
      </div>
   </div>
</div>

Upvotes: 2

Related Questions