typescript Angular 2 TypeError:无法读取未定义的属性“toUpperCase”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43514523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:28:33  来源:igfitidea点击:

Angular 2 TypeError: Cannot read property 'toUpperCase' of undefined

angulartypescriptjhipster

提问by Chebbi Ala Eddine

I am developing a medical website , i want show alert in page of medicament.component.HTML when the quantity of medicaments is < 50 . I did this :

我正在开发一个医疗网站,当药物数量小于 50 时,我想在 medicament.component.HTML 的页面中显示警报。我这样做了:

 <span *ngFor="let medicament of medicaments" >
        <span *ngIf="{{medicament.quantity}} < 50">
             <div class="callout callout-danger lead">
          <h4>Alert!</h4>
          <p>Vous devez charger le stock de medicaments {{medicament.nom}}                         
  de type {{medicament.type}}  </p>
        </div>
        </span></span>

But i don't know why it doesn't work , the error is :

但我不知道为什么它不起作用,错误是:

       > Unhandled Promise rejection: Template parse errors:
        TypeError: Cannot read property 'toUpperCase' of undefined ("<span              
       *ngIf="medicaments">
    <span *ngFor="let medicament of medicaments" >
        <span [ERROR ->]*ngIf="{{medicament.quantity}} < 50">
             <div class="callout callout-danger lead">
       "): MedicamentComponent@26:18
     Can't bind to '*ngIf' since it isn't a known property of 'span'.             
     ("<span *ngIf="medicaments">
    <span *ngFor="let medicament of medicaments" >
        <span [ERROR ->]*ngIf="{{medicament.quantity}} < 50">
             <div class="callout callout-danger lead">
      "): MedicamentComponent@26:18

回答by Sasxa

You shouldn't use interpolation {{ }}in ngIf; it expects an expression:

你不应该{{ }}在 ngIf 中使用插值;它需要一个表达式:

<span *ngIf="medicament.quantity < 50">

回答by Merx

ngIf is already in the angular context, which means, that you must remove the brackets {{}}

ngIf 已经在 angular 上下文中,这意味着您必须删除方括号 {{}}

回答by Jai

Here in your logs you can see the point of error:

在您的日志中,您可以看到错误点:

 <span [ERROR ->]*ngIf="{{medicament.quantity}} < 50">

and it is because you are using an expression on a property where you shouldn't. You just need to use property you are comparing:

这是因为您在不应该使用的属性上使用了表达式。您只需要使用要比较的属性:

 <span *ngIf="medicament.quantity < 50">