簡體   English   中英

無法綁定到“ngValue”,因為它不是“option”的已知屬性 angular cli 13

[英]Can't bind to 'ngValue' since it isn't a known property of 'option' angular cli 13

我在用戶界面上使用(Iuser)

<app-user [user]="user"></app-user>
<select (change)="onSelectChange($event.target.value)">
    <ng-container *ngFor="let user of users">
        <ng-container *ngIf="user.age>30">
            <option [ngValue]="user">
                {{user.name}} {{user.age}}
            </option>
        </ng-container>

    </ng-container>
</select>

它有 2 個錯誤:

1.

<select (change)="onSelectChange($event.target.value)">(Object is possibly 'null'.ngtsc(2531)
app.component.ts(3, 46): Error occurs in the template of component AppComponent.)
<option [ngValue]="user">(Can't bind to 'ngValue' since it isn't a known property of 'option'.ngtsc(-998002)
app.component.ts(3, 46): Error occurs in the template of component AppComponent.)

我在這里寫 mt user.ts 代碼

import { Component, Input, OnChanges, OnInit, SimpleChanges } from '@angular/core';
import { IUser } from '../app-interface';

@Component({
  selector: 'app-user',
  templateUrl: './user.component.html',
  styleUrls: ['./user.component.css']
})
export class UserComponent implements OnInit  {

 
  private _user!: IUser;
  @Input()
  set user(user:IUser){
    this._user=user;
    this.cunter++;
  };

  
  public get user() :IUser{
    return this._user;
  }
  
 

  cunter:number=0;

  constructor() { }

  ngOnInit(): void {
    console.log(this._user);
    
  }
  
}

我不知道如何解決這個問題?

為防止錯誤 #1 添加感嘆號 (.) 以表明您知道它可能為空。

<select (change)="onSelectChange($event.target.value!)">

為防止錯誤 #2,請遵循 @Abru007 的建議:

<option [value]="user">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM