Search code examples
angularangular-directiveangular-componentsproperty-binding

Bind custom component directive to object in for loop


I am trying to loop over a list of components that I create in code in my component class in my view and then use a directive to display that component in the HTML.

How can I bind my directive to the instance of the component from the list:

Here's my parent component where I create the list of the child components in code:

import { Component, OnInit } from '@angular/core';
import { IFile } from '../Interfaces/IFile';
import { MatFileComponent } from './mat-file.component';

@Component({
  selector: 'mat-file-upload',
  templateUrl: './mat-file-upload.component.html',
  styleUrls: ['./mat-file-upload.component.css']
})
export class MatFileUploadComponent implements OnInit {
  constructor() { }
  fileList: IFile[]
  addFilesToList(files: File[]): void {
    this.fileList = [];
    for (let file of files) {
      let fileComponent = new MatFileComponent()
      fileComponent.fileData = file;
      fileComponent.fileDescription = 'this is my cool description';
      fileComponent.fileName = file.name;
      fileComponent.fileType = file.type;
      this.fileList.push(fileComponent);
    }
    console.log(this.fileList);
  }
  ngOnInit() {
  }
}

Here's the html template for that component where I"m struggling to to figure out how to bind the actual instance of my children components:

<input type="file" #file multiple id="singleFile" (change)="addFilesToList(file.files)" />
<div *ngIf="fileList && fileList.length">
  <mat-file *ngFor="let file of fileList" [howDoIBindThisTo]="file"></mat-file>
</div>

Here's my individual file component:

import { Component, OnInit } from '@angular/core';
import { IFile } from '../Interfaces/IFile';

@Component({
  selector: 'mat-file',
  templateUrl: './mat-file.component.html',
  styleUrls: ['./mat-file.component.css']
})
export class MatFileComponent implements OnInit, IFile {
  public fileName: string;
  public fileDescription: string;
  public fileData: File;
  public fileType: string;
  componentLoaded: boolean = false

  constructor() {}

  ngOnInit() {
    this.componentLoaded = true;
  }
}

and here is it's corresponding HTML template:

<div *ngIf="componentLoaded">
  {{ fileType }} that's the type, here's the desc {{fileDescription}} and name {{fileName}}
</div>

Solution

  • You could create four input parameters to your component

    export class MatFileComponent implements OnInit, IFile {
      @Input() public fileName: string;
      @Input() public fileDescription: string;
      @Input() public fileData: File;
      @Input() public fileType: string;
      componentLoaded: boolean = false
    
      constructor() {}
    
      ngOnInit() {
        this.componentLoaded = true;
      }
    }
    

    Now bind it using:

    <div *ngIf="fileList && fileList.length">
      <mat-file *ngFor="let file of fileList" 
         [fileName]="file.fileName"
         [fileDescription]="file.fileDescription"
         [fileData]="file.fileData"
         [fileType]="file.fileType"
      ></mat-file>
    </div>