Angular 4 功能只能从第二次开始工作
Angular 4 function works only from second time
我正在 Angular 4 中创建购物车,并想检查 prod 中是否存在新产品 cartProducts 数组。
这是我的组件:
组件
import { Component, OnInit } from '@angular/core';
import { Router } from "@angular/router";
import { ProductsService } from '../service/products.service';
@Component({
selector: 'app-store',
templateUrl: './store.component.html',
styleUrls: ['./store.component.css']
})
export class StoreComponent implements OnInit {
itemCount: number;
cartProducts: any = [];
productsList = [];
constructor( private _products: ProductsService ) { }
ngOnInit() {
this.itemCount = this.cartProducts.length;
this._products.product.subscribe(res => this.cartProducts = res);
this._products.updateProducts(this.cartProducts);
this._products.getProducts().subscribe(data => this.productsList = data);
}
addToCart(prod){
this.cartProducts.hasOwnProperty(prod.id) ? console.log("Added yet!") : this.cartProducts.push(prod);
console.log(this.cartProducts)
}
}
我的 addToCart 函数通过点击触发正常工作,但只能从第二次开始。
1 次点击 - 我们在空的 cartProducts 数组中添加一个产品,产品被添加
2 click - 虽然添加了产品,但再次添加,现在数组中有两个相同的产品。我有两个相同产品的数组。
3 次点击 - 控制台显示 "Added yet!",现在它识别出产品在数组中了。
UPD
该产品是一个类型的对象:
{
"id" : "1",
"title" : "Title 1",
"color" : "white"
}
如何解决这个问题?
hasOwnProperty 用于检查对象中是否存在键,您将其用于数组。改用这个:
addToCart(prod){
this.cartProducts.indexOf(prod) > -1 ? console.log("Added yet!") : this.cartProducts.push(prod);
console.log(this.cartProducts)
}
试试这个:
let idx = this.cartProducts.findIndex(elem => {
return prod === elem
})
if (idx !== -1) {
console.log("Added yet!")
} else {
this.cartProducts.push(prod);
}
我正在 Angular 4 中创建购物车,并想检查 prod 中是否存在新产品 cartProducts 数组。 这是我的组件:
组件
import { Component, OnInit } from '@angular/core';
import { Router } from "@angular/router";
import { ProductsService } from '../service/products.service';
@Component({
selector: 'app-store',
templateUrl: './store.component.html',
styleUrls: ['./store.component.css']
})
export class StoreComponent implements OnInit {
itemCount: number;
cartProducts: any = [];
productsList = [];
constructor( private _products: ProductsService ) { }
ngOnInit() {
this.itemCount = this.cartProducts.length;
this._products.product.subscribe(res => this.cartProducts = res);
this._products.updateProducts(this.cartProducts);
this._products.getProducts().subscribe(data => this.productsList = data);
}
addToCart(prod){
this.cartProducts.hasOwnProperty(prod.id) ? console.log("Added yet!") : this.cartProducts.push(prod);
console.log(this.cartProducts)
}
}
我的 addToCart 函数通过点击触发正常工作,但只能从第二次开始。
1 次点击 - 我们在空的 cartProducts 数组中添加一个产品,产品被添加
2 click - 虽然添加了产品,但再次添加,现在数组中有两个相同的产品。我有两个相同产品的数组。
3 次点击 - 控制台显示 "Added yet!",现在它识别出产品在数组中了。
UPD 该产品是一个类型的对象:
{
"id" : "1",
"title" : "Title 1",
"color" : "white"
}
如何解决这个问题?
hasOwnProperty 用于检查对象中是否存在键,您将其用于数组。改用这个:
addToCart(prod){
this.cartProducts.indexOf(prod) > -1 ? console.log("Added yet!") : this.cartProducts.push(prod);
console.log(this.cartProducts)
}
试试这个:
let idx = this.cartProducts.findIndex(elem => {
return prod === elem
})
if (idx !== -1) {
console.log("Added yet!")
} else {
this.cartProducts.push(prod);
}