在Angular中使用*ngIf进行嵌套formGroup值的判断,可以通过以下步骤来实现:
首先,在组件的模板文件中,创建formGroup和formControl:
然后,在组件的类文件中,初始化formGroup和formControl:
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder } from '@angular/forms';
@Component({
selector: 'app-my-component',
templateUrl: './my-component.component.html',
styleUrls: ['./my-component.component.css']
})
export class MyComponentComponent implements OnInit {
myForm: FormGroup;
constructor(private formBuilder: FormBuilder) { }
ngOnInit() {
this.myForm = this.formBuilder.group({
nestedGroup: this.formBuilder.group({
nestedValue: ['']
})
});
}
}
最后,在模板文件中使用*ngIf进行嵌套formGroup值的判断:
在上述代码中,我们使用myForm.get('nestedGroup.nestedValue').value
来获取嵌套formGroup的值,并在*ngIf中进行判断。如果嵌套值等于'example',则显示"Nested value is 'example'"。