2017-04-20 128 views
1

我正在開發一個醫療網站,當藥物的數量爲< 50時,我想在medicament.component.HTML的頁面上顯示警報。 我這樣做:Angular 2 TypeError:無法讀取未定義的屬性'toUpperCase'

<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> 

但我不知道爲什麼它不工作,錯誤的是:

 > 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"> 
     "): [email protected]: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"> 
     "): [email protected]:18 
+0

在這串你嘗試使用toUpperCase?你可以提供你的控制器或任何其他的JS? – Sandwell

回答

8

你不應該在ngIf使用插值{{ }};它期望的表達式:

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

謝謝,但多麼可怕的錯誤信息! – duhaime

0

在這裏你的日誌,你可以看到錯誤的觀點:

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

,這是因爲你使用一個屬性的表達式,你不應該。你只需要使用屬性您比較:

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

ngIf已經在角度而言,這意味着,你必須刪除括號{{}}

相關問題