Home > Software design >  Can't bind to 'ngValue' since it isn't a known property of 'option' an
Can't bind to 'ngValue' since it isn't a known property of 'option' an

Time:05-23

i use on interface for user(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>

it has 2 error:

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.)

here i write mt user.ts code

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);
    
  }
  
}

i don't know how i can fixed this problem?

CodePudding user response:

to prevent error#1 add the exclamation sign (!) to indicate that you know it might be null.

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

to prevent error#2 follow the suggestion of @Abru007:

<option [value]="user">
  • Related