【问题标题】:Angular - Calling a pipe within click eventAngular - 在点击事件中调用管道
【发布时间】:2020-01-30 04:35:43
【问题描述】:

我在尝试找到在(单击)事件中使用管道的最佳方式时遇到问题。我收到“动作表达式中不能有管道”错误。我希望能够在用户单击按钮时触发管道。该程序的目标是当用户单击“CM”按钮时,长度列中的值将转换为厘米。任何有关解决此错误或找到更好解决方案的帮助将不胜感激。

表格

                       If user selects centimeters

in cm                         in cm
--------------------          ---------------        
User |Item  | Length        User | Item  | Length 
--------------- ---->      ---------------
User1|Item1 | 12 cm         User1| Item1 | 12 cm
User2|Item2 | 2 in          User2| Item2 | 5.08 cm

管道

@Pipe({name:convertToCmPipe})
export class ConvertToCmPipe implements PipeTransform {
 constructor(public log:LoggingService){}
 transform(value:number,newVal: string): any {
   return newVal === undefined ? value : value * 2.54;

   }
}

打字稿

onBtnSelect(x: number)
{
  return this.convertToCmPipe.transform(x,x);
}

HTML

<button (click)="onBtnSelect(lengthVal | convertToCmPipe: 'length')"
title="cm"> CM </button>

【问题讨论】:

  • This 可以帮忙吗?

标签: html angular typescript events pipe


【解决方案1】:

试试这个:

在html中:

<button (click)="onBtnSelect()"> change </button>
{{lengthVal | (isClicked? (convertToCmPipe: 'length') : '')}}

在ts中:

isClicked = false;
onBtnSelect() {
  this.isClicked = !this.isClicked;
}

不确定什么会起作用,请提供反馈。

【讨论】:

    猜你喜欢
    • 2016-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-22
    • 1970-01-01
    • 2012-05-17
    • 1970-01-01
    相关资源
    最近更新 更多