在Angular 8中,你可以使用ngx-chips库来实现表单验证。以下是一个解决方法的示例代码:
npm install ngx-chips
import { NgxChipsModule } from 'ngx-chips';
@NgModule({
imports: [
NgxChipsModule
]
})
import { Component } from '@angular/core';
@Component({
selector: 'app-my-component',
template: '...',
styleUrls: ['./my-component.component.css']
})
export class MyComponent {
tags: string[];
onSubmit() {
// 在这里执行表单提交时的操作
}
}
这样,当用户未输入标签时,将会显示一个错误消息,并且提交按钮会在表单无效时禁用。
希望这个示例对你有所帮助!