【发布时间】:2020-07-10 03:54:36
【问题描述】:
我是这里的新手,我有问题要解决。请帮忙,提前谢谢。
我通常像这样在屏幕上显示数据,而且每次都能正常工作。
<h3> Kod Bar : {{ article.barcode_no }} </h3>
但现在我想检查使用 *ngIf 如果 article.barcode_no 以某个字符开头,我希望在显示它之前先将其删除。
<h3 *ngIf=" article.article.indexOf(article.barcode_no) === 'S' ? (article.barcode_no).substr(1) : none "> Kod Bar : {{ article.barcode_no }} </h3>
但我不断收到错误
错误类型错误:无法读取未定义的属性“indexOf”
我也尝试使用 Pipe 但无法正常工作。请帮忙。谢谢。
<h3 *ngIf=" (article.barcode_no | startsWith : 'S') ? (article.barcode_no).substr(1) : none "> Kod Bar : {{ article.barcode_no }} </h3>
.
import { Pipe, PipeTransform, Injectable } from '@angular/core';
@Pipe({
name: 'startsWith'
})
export class AccessProviders implements PipeTransform {
transform(fullText: string, textMatch: string) : boolean {
return fullText.startsWith(textMatch);
}
}
[谢谢你们的回复。我感谢所有的答案! ]
【问题讨论】:
-
使用
str.chatAt(0) == 'YourChar' ? true : false; -
试试@NajamUsSaqib 的回答很简单
-
我这样使用它 "
Kod Bar : {{ article.barcode_no.chatAt(0) == 'S' ? article.barcode_no.substring(1): article.barcode_no }}
”并得到错误“ERROR TypeError: _v.context.$implicit.barcode_no.chatAt is not a function”
标签: angular ionic-framework pipe angular-ng-if