亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Angular 9 - 表單組問題

Angular 9 - 表單組問題

jeck貓 2022-11-27 16:09:58
免責聲明: 一般來說,我是 Angular 和 TS 的新手。我想要完成的是一個激活組件,如果它在 URL 參數查詢中,它要么直接向服務提交代碼。如果沒有,用戶可以將代碼插入表單并以這種方式將其提交給服務。當 URL 查詢中有代碼時該字段被禁用,沒有時啟用,因此工作正常。我遇到的問題與提交服務有關。我收到以下錯誤;ERROR TypeError: Cannot read property 'get' of undefined at ActivationComponent.handleActivation (activation.component.ts:48)在第 48 行,我this.validateform.value過去曾導致同樣的錯誤。我已經搞砸了這么長時間,嘗試了不同的方法,但我有點迷路了……這是 component.ts:import {Component, Input, Output, EventEmitter, AfterViewInit, OnInit} from '@angular/core';import {ActivatedRoute, Router} from '@angular/router';import {FormBuilder, FormControl, FormGroup, Validators} from '@angular/forms';import {AuthService} from '../auth/auth.service';@Component({  selector: 'app-activation',  templateUrl: './activation.component.html',  styleUrls: ['./activation.component.scss']})export class ActivationComponent implements OnInit {  validateForm!: FormGroup;  showModal = false;  isConfirmLoading = false;  activationCode: string;  error: string;  success: string;  constructor(private router: Router,              private fb: FormBuilder,              private activatedRoute: ActivatedRoute,              private authService: AuthService) {    this.activatedRoute.queryParams.subscribe(params => {      const code = params.code;      if (code){        this.isConfirmLoading = true;        this.handleActivation(code);      }    });  }  ngOnInit() {    this.showModal = true;    this.validateForm = new FormGroup({      CODE: new FormControl({value: this.activationCode, disabled: this.isConfirmLoading}, Validators.required)    });  }  handleActivation(code){    this.error = null;    this.activationCode = code;    if (code === null){      if (!this.validateForm.valid) {        console.log('Invalid Form');        return;      }    }    this.authService.activate(this.validateForm.get('CODE'))      .subscribe(data => {        console.log('SUCCESS!');        console.log(data);      },        error => {        console.log('ERROR!');        console.log(error);        });  }
查看完整描述

1 回答

?
阿晨1998

TA貢獻2037條經驗 獲得超6個贊

在這里,構造函數中的代碼在執行 ngOnInit() 方法之前執行,因此沒有初始化表單,您正在嘗試使用該表單。


因此,只需更改您的訂閱位置,如下所示。這肯定會解決您的問題。


     constructor(private router: Router,

              private fb: FormBuilder,

              private activatedRoute: ActivatedRoute,

              private authService: AuthService) {

   

  }


  ngOnInit() {

    this.showModal = true;

    this.validateForm = new FormGroup({

      CODE: new FormControl({value: this.activationCode, disabled: this.isConfirmLoading}, Validators.required)

    });

 this.activatedRoute.queryParams.subscribe(params => {

      const code = params.code;

      if (code){

        this.isConfirmLoading = true;

        this.handleActivation(code);

      }

    });

  }


查看完整回答
反對 回復 2022-11-27
  • 1 回答
  • 0 關注
  • 159 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號