Angular – 如何解决针对 FormGroup 的对象可能为 null 的问题

huangapple go评论93阅读模式
英文:

Angular - How to resolve object is possibly null for a FormGroup

问题

在Angular-15项目中,我在component.ts文件中找到了以下代码:

component.ts:

  1. export class CountryCreateComponent {
  2. countriesDatas: any[] = [];
  3. selectedCountryCode: string = '';
  4. selectedCountryName: string = '';
  5. createCountryForm!: FormGroup;
  6. constructor(
  7. private fb: FormBuilder,
  8. private toastr: ToastrService
  9. ) {
  10. }
  11. createCountry() {
  12. this.createCountryForm = this.fb.group({
  13. CountryCode: ['', [Validators.required]],
  14. CountryName: [''],
  15. });
  16. }
  17. createValidate() {
  18. if (!this.createCountryForm.valid) {
  19. this.createCountryForm.markAllAsTouched();
  20. return;
  21. }
  22. }
  23. get fc() {
  24. return this.createMerchantForm.controls;
  25. }
  26. constructor(
  27. private fb: FormBuilder
  28. ) {}
  29. ngOnInit(): void {;
  30. }
  31. onCountryChange() {
  32. const selectedCountry = this.countriesDatas.find(selectedCountryCode => {
  33. return selectedCountryCode.country_code === this.form.get('countryCode').value;
  34. });
  35. if (selectedCountry) {
  36. this.createCountryForm.get('countryCode').setValue(selectedCountryCode.country_code);
  37. this.createCountryForm.get('countryName').setValue(selectedCountryName.country_name);
  38. }
  39. }
  40. }
  41. }

然而,我遇到了这个错误:object is possibly null

然后,在 this.createCountryForm.get 中,createCountryForm 被标记为问题点。

如何解决这个问题呢?

英文:

In Angular-15 project I have this code in the component.ts:

component.ts:

  1. export class CountryCreateComponent {
  2. countriesDatas: any[] = [];
  3. selectedCountryCode: string = '';
  4. selectedCountryName: string = '';
  5. createCountryForm!: FormGroup;
  6. constructor(
  7. private fb: FormBuilder,
  8. private toastr: ToastrService
  9. ) {
  10. }
  11. createCountry() {
  12. this.createCountryForm = this.fb.group({
  13. CountryCode: ['', [Validators.required]],
  14. CountryName: [''],
  15. });
  16. }
  17. createValidate() {
  18. if (!this.createCountryForm.valid) {
  19. this.createCountryForm.markAllAsTouched();
  20. return;
  21. }
  22. }
  23. get fc() {
  24. return this.createMerchantForm.controls;
  25. }
  26. constructor(
  27. private fb: FormBuilder
  28. ) {}
  29. ngOnInit(): void {;
  30. }
  31. onCountryChange() {
  32. const selectedCountry = this.countriesDatas.find(selectedCountryCode => {
  33. return selectedCountryCode.country_code === this.form.get('countryCode').value;
  34. });
  35. if (selectedCountry) {
  36. this.createCountryForm.get('countryCode').setValue(selectedCountryCode.country_code);
  37. this.createCountryForm.get('countryName').setValue(selectedCountryName.country_name);
  38. }
  39. }
  40. }
  41. }

However, I got this error: object is possibly null

Then createCountryForm is highlighted in this.createCountryForm.get

How do I resolve this?

答案1

得分: 1

你可以使用"nullish coalescing operator" (??) 或可选链 (?.) 来处理变量可能为null的情况。

  1. this.createCountryForm.get('countryCode') ?? {value:''/*默认值*/}.value

  1. this.createCountryForm.get('countryCode')?.value

关于为什么formControl可能为null的详细解释请查看这里

英文:

You can use the "nullish coalescing operator" (??) or optional chaining (?.) to handle the possibility of the variable being null.

  1. this.createCountryForm.get('countryCode') ?? {value:''/*default Value*/}.value

or

  1. this.createCountryForm.get('countryCode')?.value

For Detailed Explanation why formControl can be null check here

答案2

得分: 0

"Since you don't use a typed form, this this.createCountryForm.get('countryCode') (and the other too) can lead to a null object. Put a ! after it: this.createCountryForm.get('countryCode')!."

英文:

Since you don't use a typed form, this this.createCountryForm.get('countryCode') (and the other too) can lead to a null object. Put a ! after it: this.createCountryForm.get('countryCode')!.

huangapple
  • 本文由 发表于 2023年5月23日 01:08:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76308495.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定