Angular 如何从中获取FormControl的值并将其作为参数传递给函数?

Angular 如何从中获取FormControl的值并将其作为参数传递给函数?,angular,reactive-programming,Angular,Reactive Programming,我有以下模板: <form [formGroup]="firstFormGroup" #formone="ngForm"> <ng-template matStepLabel>Saisir l'url </ng-template> <mat-form-field fxFlex> <input matInput placeholder="Url" formControlName="su

我有以下模板:

 <form [formGroup]="firstFormGroup"  #formone="ngForm">
        <ng-template matStepLabel>Saisir l'url </ng-template>
        <mat-form-field fxFlex>
            <input matInput placeholder="Url" formControlName="subscriptionUrl" id="subscriptionUrl" required>
        </mat-form-field>


        <div class="card">
            <div class="card-body">
                <div class="row">
                    <div>
                        <button type="button" mat-raised-button (click)="check()">
                            Calculer
                        </button>
                    </div>


                </div>
                <div class="row">
                    <div *ngIf="priceFound != 0">
                        <div class="col-md-6 grid-margin stretch-card">
                            <mat-form-field fxFlex>
                                <input matInput placeholder="trouvé" formControlName="Found" id="priceFound" required>
                                {{Found}}
                            </mat-form-field>
                        </div>
                        <button type="button" mat-raised-button matStepperNext>Valider  et passer à
                            l'étape suivante
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </form>
在名为subscriptionUrl的表单控件中设置值时,无法获取其值typescript端。 我想获取这个字段的值,并将其作为参数传递给函数类型脚本端以处理它

错误是:

SouscriptionComponent.html:14 ERROR TypeError: Cannot read property 'value' of undefined

谢谢你的帮助。

你拿不到。因为这条语句
subscriptionUrl:FormControl
除了类型声明之外什么都不做。因此,
this.subscriptionUrl
未定义,您可以从中读取属性“
”。请描述您的目标,并创建一个易于理解的变量。

继续我的评论:您声明了一个变量

subscriptionUrl: FormControl;
但是你再也不用它了。你一次也不写

this.subscriptionUrl = ...;
这意味着您的变量
未定义
,因此不包含

要获取值,请执行以下操作

this.firstFormGroup.get('subscriptionUrl').value;
或者在声明表单后添加此项

this.subscriptionUrl = this.firstFormGroup.get('subscriptionUrl');
回答您:,创建表单不会实例化组件中的变量。要实例化变量,必须使用

this.subscriptionUrl = ...;

此.subscriptionUrl.value未定义,因为您只声明了它。尝试给它一个值。因此,您也在使用模板驱动的表单
#formone=“ngForm”
?您需要使用这个.firstFormGroup.get('subscriptionUrl')。value@trichetriche我想错误是因为它无法找到提取值的元素。@trichetriche我认为this.firstFormGroup=this.\u formBuilder.group({subscriptionUrl:new FormControl(“”,[Validators.required])是一个声明。我错了吗?
subscriptionUrl:new FormControl(“”,[Validators.required])),
声明为空,不是吗?这意味着,您在FormGroup
firstFormGroup
下生成了一个名为
subscriptionUrl
的全新FormControl。它不会对顶部声明的现有变量
subscriptionUrl:FormControl
产生任何影响。问题已通过:
console.log解决('this.firstFormGroup.get(“subscriptionUrl”)'+this.firstFormGroup.get('subscriptionUrl').value);
this.subscriptionUrl = ...;