0

Good day Community, I have a problem with binding interpolated data/value with [ngModel], I would like to send the interpolated data to my database so i can get it and use it, but its sending and returning returning an empty string. My cart.component.html

 <div class="col-5 col-md-6">
                <h5>
                  <input type="text"  class="form-control user-address goat"
                    name="item"  placeholder="" [value]= "item.title" [(ngModel)]="cart.item.title" required >
                  <a routerLink="/products/shopping-hub/{{ item._id }}">{{ item.title }}</a>
                </h5>
                
                <p class="m-0">
                  <input type="text"  class="form-control user-address goat"
                  name="owner"  placeholder="" [value]= "item.owner.name" [(ngModel)]="cart.item.owner.name" required >
                  <small class="text-muted">{{ item.owner.name }}</small>
                </p>
                <p class="m-0">
                  <input type="text"  class="form-control user-address goat"
                  name="category"  placeholder="" [value]= "cart.item.category.name" [(ngModel)]="item.catgory.name" required >
                  <small class="text-muted">{{ item.category.name }}</small>
                </p>
                <a class="text-danger" (click)="removeProduct(i, item)">Remove from Cart</a>
              </div>
              <div class="col-1 col-md-2">
                <input type="number" id="price" class="form-control user-address goat"
                name="price"  placeholder="" [value]= "price" [(ngModel)]="cart.item.price" required >
                <h6 class="font-weight-bold text-danger"> ₦{{item.price}}</h6>
              </div>

and my cart.component.ts

 cart = {
    item.title: [],
    item.owner.name:[],
    item.category.name:[],
    item.price: [],
   


  async checkout() {
    this.btnDisabled = true;
    try {
      if (this.validate()) {
        const data = await this.rest.post(
          'http://localhost:3030/api/cart',
          this.cart
        ); console.log('this.cart');
        data['success']
          ? this.router.navigate(['/products/buynow/deliveryaddress/check-out']) 
            .then(() => this.data.success(data['message']))
            .catch(error => this.data.error(error))
          : this.data.error(data['message']);
        
      } else {
        this.btnDisabled = false;
      }
    } catch (error) {
      this.data.error(error);
    }
  }

Thank you

5
  • Just to make sure, do you have import { FormsModule } from '@angular/forms'; in your app module? Commented Jul 26, 2020 at 18:49
  • yes, i have FormsModule imported Commented Jul 26, 2020 at 19:46
  • Could you remove [value] and try. Only ngModel is required for data binding. Commented Jul 26, 2020 at 20:56
  • Tried removing the [value], still the same thing Commented Jul 26, 2020 at 21:08
  • Do you define cart in that way? ``` cart = { item.title: [], item.owner.name:[], item.category.name:[], item.price: [],``` If yes, i don't think that's correct. let cart = { item: {title: []}} Also, can you see if there are any errors in the console? Commented Jul 27, 2020 at 5:29

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.