javascripthtmlangularangular-fontawesome

Updating <mat-icon> from one <mat-form-field> to another doesn't consistently update the icon


I have been trying to make a form for blog posting, and I want to have selectable category icons next to the title.

I made a form with selectable font awesome icons, but when I select a category I can't select another one to change the icon again. It only changes if I "reset" it with the "None" option in-between each selection.

Doh!

However, when I change the code to use the icon name as a string, instead of as an icon input it changes with every selection, as I want it to. I just don't understand how it can update the text regularly but not the icon.

oh, c'mon!

This is my actual code:
[create.component.html]

...
<div class="blog-container">
  <mat-card class="blog-card">
    <section>
      <h2>Create blog post</h2>
    </section>
    <form [formGroup]="createForm" class="width-1">

      <div class="width-1">
        <mat-form-field class="blog-title" appearance="outline">
          <mat-label>Post Title*</mat-label>
          <input matInput formControlName="newTitle" #newTitle>
          <mat-icon matSuffix *ngIf="category" [fontSet]="category.set" [fontIcon]="category.icon"></mat-icon>
          <mat-hint>the first two fields are required</mat-hint>
        </mat-form-field>
      </div>

      <div class="width-1">
        <mat-form-field class="blog-text" appearance="outline">
          <mat-label>Bread Text*</mat-label>
          <textarea matInput rows="2" formControlName="newText" #newText></textarea>
        </mat-form-field>
      </div>

      <div class="width-1 blog-row">
        <mat-form-field class="blog-author" appearance="fill">
          <mat-label>Author</mat-label>
          <input matInput formControlName="newAuthor" #newAuthor>
        </mat-form-field>

        <mat-form-field class="blog-category" appearance="fill">
          <mat-label>Category</mat-label>
          <mat-select [(value)]="category" formControlName="newCategory" #newCategory>
            <mat-select-trigger *ngIf="category">
              <span>{{category.name}}</span>
            </mat-select-trigger>
            <mat-option [value]="null">
              <span>None</span>
            </mat-option>
            <mat-option *ngFor="let category of categories" [value]="category">
              <mat-icon [fontSet]="category.set" [fontIcon]="category.icon"></mat-icon>
              <span>{{category.name}}</span>
            </mat-option>
          </mat-select>
        </mat-form-field>
      </div>

      <div class="width-1 center">
        <button mat-raised-button color="primary" routerLink="/archive">BACK</button>
        <button type="submit" (click)="addPost(title.value, text.value, author.value, category.value)" [disabled]="createForm.pristine || createForm.invalid" mat-raised-button color="primary">POST</button>
      </div>
    </form>
  </mat-card>
</div>
...

[create.component.ts]

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

import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { Router } from '@angular/router';
import { PostService } from '../../post.service';

@Component({
  selector: 'app-create',
  templateUrl: './create.component.html',
  styleUrls: ['./create.component.scss']
})
export class CreateComponent implements OnInit {

  createForm: FormGroup;

  category: CategoryDTO = null;
  categories: CategoryDTO[] = [
    new CategoryDTO({name: 'Programming', set: 'fas', icon: 'fa-laptop-code'}),
    new CategoryDTO({name: 'Croshetting', set: 'fas', icon: 'fa-cut'}),
    new CategoryDTO({name: 'Arts/Crafts', set: 'fas', icon: 'fa-tools'})
  ];

  constructor(private postService: PostService, private fb: FormBuilder, private router: Router) {
    this.createForm = this.fb.group({
      newTitle: ['', Validators.required],
      newText: ['', Validators.required],
      newAuthor: '',
      newCategory: ''
    });
  }

  addPost(newTitle, newText, newAuthor, newCategory){
    this.postService.addPost(newTitle, newText, newAuthor, newCategory).subscribe(() => {
      this.router.navigate(['/archive']);
    });
  }

  ngOnInit() {
  }

}

class CategoryDTO {
  name: string;
  set: string;
  icon: string;
  constructor(category?: any) {
    this.name = category && category.name || null;
    this.set = category && category.set || null;
    this.icon = category && category.icon || null;
  }
}

Since I can't load the Font Awesome CSS in StackBlitz (as far as I know), this is the closest I could come to reproducing a manageable code there.

I am new to Angular.


Solution

  • I imported FormsModule and CommonModule (FontAwesomeModule was already there) in my create.component.ts file, and I re-added node_modules/@fortawesome/fontawesome-free/css/all.min.css in "styles: [...]" in angular.json, and that did the trick!
    As far as I can tell, having import { faCut, faCode, faTools } from '@fortawesome/free-solid-svg-icons'; in create.component.ts makes no difference.
    Thanks to "Allabakash" for pointing me in the right direction. :)