[英]Type 'Event' is not assignable to type 'string'. angular typescript error
I'm very new to Angular and I've come across with a typescript problem.我对 Angular 很陌生,我遇到了 typescript 问题。
This is the error that I'm getting:这是我得到的错误:
Error: contact/contact.component.html:70:37 - error TS2322: Type 'Event' is not assignable to type 'string'.
<mat-radio-group [(value)]="contactGender">
<mat-radio-button value="male">Male</mat-radio-button>
contact/contact.component.ts:6:16
templateUrl: './contact.component.html',
The part of my HTML component I'm getting the error on:我的 HTML 组件的一部分出现错误:
<mat-label>Gender</mat-label>
<mat-radio-group [(value)]="contactGender">
<mat-radio-button value="male">Male</mat-radio-button>
<mat-radio-button value="female">Female</mat-radio-button>
</mat-radio-group>
My TS component:我的 TS 组件:
import { Component} from '@angular/core';
import { FormControl } from '@angular/forms';
import {MatDatepickerModule} from '@angular/material/datepicker';
@Component({
selector: 'app-contact',
templateUrl: './contact.component.html',
styleUrls: ['./contact.component.css']
})
export class ContactComponent {
date = new FormControl(new Date());
serializedDate = new FormControl((new Date()).toISOString());
contactGender: string = "female";
public saveContact(){
/* Typically this method will be used to send the contact form to a server to save it*/
}
constructor() { }
}
Where am I going wrong?我哪里错了?
Change the HTML component that's giving the error to this.更改导致此错误的 HTML 组件。 Here we bind the variable using ngModel not the value and put property binding on the value using []在这里,我们使用 ngModel 而不是值绑定变量,并使用 [] 将属性绑定到值上
<mat-label>Gender</mat-label>
<mat-radio-group [(ngModel)]="contactGender">
<mat-radio-button [value]="male">Male</mat-radio-button>
<mat-radio-button [value]="female">Female</mat-radio-button>
</mat-radio-group>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.