首页 > 解决方案 > Child.html:28 错误类型错误:无法读取未定义的属性“推送”

问题描述

如何在 typescript angular 6 中使用 push 如何在 typescript angular 6 中使用 push 如何在 typescript angular 6 中使用 push 如何在 typescript angular 6 中使用 push

This error is shown while searching for product, How to use push 

“Child.html:28 错误类型错误:无法读取未定义的属性‘推送’”

<h2>Hello Iam CHild Heading</h2>


<button (click)="search()">Search</button>
<h1>{{msg}}</h1>
<h1>{{msg1}}</h1>
<p></p>

<h1>Products</h1>
<table>
    <tr>
    <th>Sr.No.</th>
    <th>Product Id</th>
    <th>Product Name</th>
    <th>Product Price</th>
    </tr>
    <tr *ngFor='let product of products; let i=index;'>    
        <td>
            {{i+1}}
        </td>
        <td>
            {{product.pid}}
        </td>
        <td>
            {{product.pname}}
        </td>
        <td>
            {{product.price}}
        </td>
    </tr>
</table>

<!-- <div class="childDiv">HIII</div>


<p>Company Name {{rcname}}</p>
<p>Company Address {{raddress}}</p> -->









import { Component, Input } from "@angular/core";
import { NgModel } from "@angular/forms";
// import { productlist } from "../directive_demo/for_demo/productlist.component";

@Component({
  selector: "app-child",
  templateUrl: "./child.component.html",
  styleUrls: ["./child.component.css"]
})
export class Child {
  // @Input() rcname:string;
  //   @Input() raddress:string;

  msg: string;
  msg1: string;
//   filter_prod=[];
  filter_prod:any[];

  @Input() rpname: string;

  search() {
    for (var i = 0; i < this.products.length; i++) {
      if (this.rpname === this.products[i].pname) {

        // console.log(this.products[i]);
        // this.msg =
        //   this.products[i].pname +
        //   " is available. " +
        //   "The cost for " +
        //   this.products[i].pname +
        //   " is Rs. " +
        //   this.products[i].price;
          this.filter_prod.push(this.products[i]);
      }
    }
    this.products=this.filter_prod;

    if (this.rpname != this.products[i]) {
      this.msg1 = "Not Found";
      // console.log('not found')
    }

    // let filteredArray = [];
    // for (let i = 0; i < products.length; i++)
    // {
    //     if (products[i] === 'search string')
    //     { filteredArray.push(products[i]);
    //     }
    //  }
  }

  products: any[];
  constructor() {
    this.products = [
      { pid: 1001, pname: "Mobile", price: 10000, qty: 5, discount: 10 },
      { pid: 1002, pname: "Mobile", price: 20000, qty: 5, discount: 20 },
      { pid: 1003, pname: "device", price: 30000, qty: 6, discount: 15 },
      { pid: 1004, pname: "Mobile", price: 40000, qty: 5, discount: 20 },
      { pid: 1005, pname: "device", price: 50000, qty: 6, discount: 15 },
      { pid: 1006, pname: "Mobile", price: 60000, qty: 5, discount: 20 },
      { pid: 1007, pname: "device", price: 70000, qty: 6, discount: 15 },
      { pid: 1008, pname: "Mobile", price: 80000, qty: 5, discount: 20 },
      { pid: 1009, pname: "device", price: 90000, qty: 6, discount: 15 },
    ];
  }
}

I want to show searched products to filter_prod but iam not able to push that in filter_prod

如何在 typescript angular 6 中使用 push 如何在 typescript angular 6 中使用 push 如何在 typescript angular 6 中使用 push

标签: javascriptangularpush

解决方案


您对代码中的注释是正确的:

filter_prod:any[] = [];

推荐阅读