簡體   English   中英

更新中<mat-icon>從一個<mat-form-field>到另一個不會持續更新圖標

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

我一直在嘗試制作一個用於博客發布的表格,我希望標題旁邊有可選擇的類別圖標。

我制作了一個帶有可選字體很棒圖標的表單,但是當我選擇一個類別時,我無法選擇另一個類別來再次更改圖標。 只有當我在每個選擇之間使用“無”選項“重置”它時它才會改變。

哦!

但是,當我更改代碼以使用圖標名稱作為字符串,而不是作為圖標輸入時,它會隨着每次選擇而改變,正如我希望的那樣。 我只是不明白它如何定期更新文本而不是圖標。

哦,來吧!

這是我的實際代碼:
[創建.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>
...

[創建.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;
  }
}

由於我無法在 StackBlitz 中加載 Font Awesome CSS(據我所知),這是我在那里重現可管理代碼的最接近的方法

我是 Angular 的新手。

我進口FormsModule和CommonModule(FontAwesomeModule已經在那里了)我create.component.ts文件,我重新添加node_modules/@fortawesome/fontawesome-free/css/all.min.css在“樣式:[...] " 在angular.json 中,就成功了!
據我所知, import { faCut, faCode, faTools } from '@fortawesome/free-solid-svg-icons'; create.component.ts中沒有區別。
感謝“Allabakash”為我指明了正確的方向。 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM