gpt4 book ai didi

javascript - 将 从一个 更新到另一个不会持续更新图标

转载 作者:行者123 更新时间:2023-11-30 06:10:06 26 4
gpt4 key购买 nike

我一直在尝试制作一个用于博客发布的表单,我希望在标题旁边有可选择的类别图标。

我制作了一个带有可选字体超棒图标的表单,但是当我选择一个类别时,我无法选择另一个类别来再次更改图标。只有当我在每次选择之间使用“无”选项“重置”它时,它才会改变。

Doh!

但是,当我更改代码以将图标名称用作字符串而不是图标输入时,它会随着每次选择而变化,正如我所希望的那样。我只是不明白它如何定期更新文本而不是图标。

oh, c'mon!

这是我的实际代码:
[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>
...

[创建.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(据我所知),this is the closest I could come to reproducing a manageable code there .

我是 Angular 新手。

最佳答案

我在我的 create.component.ts 文件中导入了 FormsModule 和 CommonModule(FontAwesomeModule 已经存在),然后我重新添加了 node_modules/@fortawesome/fontawesome-free/css/all .min.cssangular.json 的“styles: [...]”中,这就成功了!
据我所知,在 create.component.tsimport { faCut, faCode, faTools } from '@fortawesome/free-solid-svg-icons';没有区别。
感谢“Allabakash”为我指明了正确的方向。 :)

关于javascript - 将 <mat-icon> 从一个 <mat-form-field> 更新到另一个不会持续更新图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59432323/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com