【问题标题】:condition not working as spect in Angular条件在 Angular 中无法正常工作
【发布时间】:2021-01-20 21:12:10
【问题描述】:

我正在尝试使用 Angular 10 使用 asp.net core 3 api 检查数据是否存在,但只有在返回和发布时才放入,否则在 TS 代码下不起作用

handleAddToCart() {
    let checking = this.CartService.getCartItemByItemId(this.productItem.itemLookupCode, this.cartForm.value.weight, this.productItem.storeId)
    .subscribe(c=>{
      return c;
    });
    if (checking ) {
      const updateCartPost = {
        itemID: this.productItem.itemID,
        itemLookupCode: this.productItem.itemLookupCode,
        categoryID: this.productItem.categoryID,
        departmentID: this.productItem.departmentID,
        itemDescription: this.productItem.itemDescription,
        subDescription3: this.productItem.subDescription3,
        quantity: this.cartForm.value.qty,
        weight: this.cartForm.value.weight,
        snapShotPrice: this.productItem.snapShotPrice,
        storeId: this.productItem.storeId,
        barcode: this.productItem.barcode,
        email: localStorage.getItem('email'),
        itemImage: ''
      }
      this.CartService.updateCartItem(updateCartPost).subscribe(u => {
        this.msg.sendMsg(this.AddToCart(this.cartForm.value.weight));
        this.toaster.success('Added To Cart');
        console.log('sssssr');
      });

    }
else{
      this.CartService.addProductToCart(this.AddToCart(this.cartForm.value.weight)).subscribe(() => {
        this.msg.sendMsg(this.AddToCart(this.cartForm.value.weight));
        this.toaster.success('Added To Cart');
        console.log(this.AddToCart(this.cartForm.value.weight));
      });
}
  }

谁能帮帮我?

我需要对 if 和 else 使用条件,但它只返回 if 并且没有看到 else。

【问题讨论】:

  • 你不能从这样的 observable 返回。查看checking的类型,其实是Subscription。查看带有可观察对象的管道,特别是 switchMap 应该在这里使用,因为您正在链接 API 调用。

标签: typescript angular9 angular10


【解决方案1】:

您在 if 条件中检查的变量 checking 将始终为真,因为它属于订阅类型。您可能正在寻找这种逻辑:

handleAddToCart() {
let checking = this.CartService.getCartItemByItemId(this.productItem.itemLookupCode, this.cartForm.value.weight, this.productItem.storeId)
.subscribe(c=>{
  if (c ) {
  const updateCartPost = {
    itemID: this.productItem.itemID,
    itemLookupCode: this.productItem.itemLookupCode,
    categoryID: this.productItem.categoryID,
    departmentID: this.productItem.departmentID,
    itemDescription: this.productItem.itemDescription,
    subDescription3: this.productItem.subDescription3,
    quantity: this.cartForm.value.qty,
    weight: this.cartForm.value.weight,
    snapShotPrice: this.productItem.snapShotPrice,
    storeId: this.productItem.storeId,
    barcode: this.productItem.barcode,
    email: localStorage.getItem('email'),
    itemImage: ''
  }
  this.CartService.updateCartItem(updateCartPost).subscribe(u => {
    this.msg.sendMsg(this.AddToCart(this.cartForm.value.weight));
    this.toaster.success('Added To Cart');
    console.log('sssssr');
  });

}
else{
  this.CartService.addProductToCart(this.AddToCart(this.cartForm.value.weight)).subscribe(() => {
    this.msg.sendMsg(this.AddToCart(this.cartForm.value.weight));
    this.toaster.success('Added To Cart');
    console.log(this.AddToCart(this.cartForm.value.weight));
  });
}});}

【讨论】:

  • 感谢@user2007130的回复,但是每次输入if only都不存在else中
  • @mohamed 您需要验证变量“c”的值。 if(c) 可能不是正确的条件,它可能是更具体的东西,例如 if(c === true)
  • 是的,谢谢你解决了 if(c == 'true');
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-12
  • 2014-11-07
  • 1970-01-01
  • 2018-03-05
相关资源
最近更新 更多