javascript - 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
解决方案
您对代码中的注释是正确的:
filter_prod:any[] = [];
推荐阅读
- python - 如何查找具有相同名称的列的最大值
- c++ - CLIPS 系统停止并且不继续执行
- android - Android Highcharts DialogFragment
- java - 有 java -jar -P 选项吗?
- jenkins - 使用 Groovy 脚本获取给定目录中的所有文件夹(使用 Jenkins 中的扩展选择参数)
- java - android studio firebase 数据库:如何从子子节点获取数据
- python - 使用 FFMPEG 覆盖 Chromakey 后视频变暗
- ios - 如何从 SFSafariViewController 重定向回应用程序
- javascript - 如何在 Angular 中为响应式 div 设置动画?
- python - 如何填充空字符以在python中列出项目?