Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do i see [object Object] inside my input?

I can't understand why instead of a placeholder inside my input I see [object Object];

Here is my html:

<div class="input-form">
  <input type="text" placeholder="Type name here..." [(ngModel)]="newItem">
  <button (click)="addItem()">Add new</button>
</div>
<ul>
  <li *ngFor="let item of items">{{ item }}
    <button (click)="deleteItem(item)" >Delete</button>
  </li>
</ul>

And here is component.ts:

newItem = {};
items = [];

addItem() {
  if (this.newItem !== null) {
    this.items.push(this.newItem);
    this.newItem = {};
  }
}
like image 576
Somebody. Avatar asked May 21 '26 23:05

Somebody.


1 Answers

[object Object] is result of toString() function.

const obj = { };
console.log(obj.toString());

result:

[object Object]

You need to pass value of string type, but I highly recommend not to pass value with ngModel at all. You should use FormGroup to create forms.

like image 168
web.dev Avatar answered May 23 '26 12:05

web.dev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!