简体   繁体   English

类型“事件”不可分配给类型“字符串”。 angular typescript 错误

[英]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.

相关问题 角度打字稿错误:“字符串”类型无法分配给“任何[]”类型 - Angular typescript error: Type 'string' is not assignable to type 'any[]' 类型“null”不能分配给类型“string”。 角/打字稿 - Type 'null' is not assignable to type 'string'. Angular/TypeScript 键入'字符串 | string[]' 不可分配给类型 'string' - typescript 类型错误 - Type 'string | string[]' is not assignable to type 'string' - typescript type error Angular 错误类型字符串不可分配给类型 never - Angular error Type string is not assignable to type never 打字稿:不可分配给键入错误 - Typescript: is not assignable to type error 键入'{类型代码:字符串; }[]' 不能分配给类型 'string[]'。 // Angular 9.1.15, TypeScript - Type '{ typecode: string; }[]' is not assignable to type 'string[]'. // Angular 9.1.15, TypeScript 我想在 Angular 中添加局部变量,但出现错误“type 'string null' is not assignable to type 'string'. typescript” - I want to add local variable in Angular but error occur "type 'string null' is not assignable to type 'string'. typescript" Angular2 / TypeScript:错误TS2345:类型'String'的参数不能分配给'string'类型的参数 - Angular2/TypeScript: error TS2345: Argument of type 'String' is not assignable to parameter of type 'string' 当使用 Angular @Input 装饰器时,Typescript Type 'string' is not assignable to type - When using Angular @Input decorator, Typescript Type 'string' is not assignable to type 角度类型错误:无法将类型“字符串”分配给类型“字符串[]” - Angular type error: Type 'string' is not assignable to type 'string[]'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM