2025-07-09 16:55
typescript
动态设置Angular 6中的 FormControl选定的条件
typescriptimport { Component, OnInit } from '@angular/core';import { FormControl } from '@angular/forms';@Component({ selector: 'app-my-form', template: <code> <select [formControl]="myControl"> <option *ngFor="let option of options" [value]="option">{{ option }}</option> </select> </code>, styleUrls: ['./my-form.component.CSS']})export class MyFormComponent implements OnInit { myControl: FormControl; options: string[] = ['Option 1', 'Option 2', 'Option 3']; constructor() { this.myControl = new FormControl(); this.myControl.valueChanges.subscribe(value => { this.updateOptions(value); }); } ngOnInit() {} updateOptions(value: string) { // 根据条件更新选定<option>的值 if (value === 'Option 1') { this.options = ['Option 1', 'Option 2']; } else if (value === 'Option 2') { this.options = ['Option 2', 'Option 3']; } else { this.options = ['Option 3']; } }}
html<select [formControl]="myControl"> <option *ngFor="let option of options" [value]="option">{{ option }}</option></select>
关于我们|免责条款|版权声明|侵权举报|隐私政策|联系我们
Copyright © 2025 IZhiDa.com All Rights Reserved.
知答 版权所有 粤ICP备2023042255号