2016-11-10 13 views
0

<tr>のループがngForのテーブルがあります。 <select>を使用して双方向データバインディングプロパティの値と一致する<tr>のみを表示したいとします。角度2 ng要素の再レンダリングと双方向データのバインディング

アプリが最初に読み込まれるときはうまく動作しますが、selectオプションを変更すると、必要に応じて表示がレンダリングされません。

__________CODE BELOW__________

PLUNKER CODE

HTML

<label> 
    Hours 
    <select 
     [(ngModel)]="location" 
     name="location"> 
     <option *ngFor="let loc of locations" [value]="loc.id">{{loc.name}}</option> 
    </select> 
</label> 

<table> 
    <thead> 
     <tr> 
      <th>Location</th> 
      <th>Day</th> 
      <th>Open</th> 
      <th>Close</th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr *ngFor="let hour of hours"> 
      <td *ngIf="hour.locationId === location"> 
       locationId is {{hour.locationId}} 
      </td> 
      <td *ngIf="hour.locationId === location"> 
       {{hour.day}} 
      </td> 
      <td *ngIf="hour.locationId === location"> 
       {{hour.dayStart}} 
      </td> 
      <td *ngIf="hour.locationId === location"> 
       {{hour.dayEnd}} 
      </td> 
     </tr> 

    </tbody> 
</table> 

成分

hours: any[] = [ 
    { locationId: 1, day: 'Sunday', dayValue: 0, dayStart: '9:00am', dayEnd: '7:00pm', open: false, working: false }, 
    { locationId: 1, day: 'Monday', dayValue: 1, dayStart: '9:00am', dayEnd: '7:00pm', open: true, working: true }, 
    { locationId: 1, day: 'Tuesday', dayValue: 2, dayStart: '9:00am', dayEnd: '7:00pm', open: true, working: true }, 
    { locationId: 1, day: 'Wednesday', dayValue: 3, dayStart: '9:00am', dayEnd: '7:00pm', open: true, working: true }, 
    { locationId: 1, day: 'Thursday', dayValue: 4, dayStart: '9:00am', dayEnd: '7:00pm', open: true, working: true }, 
    { locationId: 1, day: 'Friday', dayValue: 5, dayStart: '9:00am', dayEnd: '7:00pm', open: true, working: true }, 
    { locationId: 1, day: 'Saturday', dayValue: 6, dayStart: '9:00am', dayEnd: '7:00pm', open: true, working: true }, 
    { locationId: 2, day: 'Sunday', dayValue: 0, dayStart: '9:00am', dayEnd: '8:00pm', open: false, working: false }, 
    { locationId: 2, day: 'Monday', dayValue: 1, dayStart: '9:00am', dayEnd: '8:00pm', open: true, working: true }, 
    { locationId: 2, day: 'Tuesday', dayValue: 2, dayStart: '9:00am', dayEnd: '8:00pm', open: true, working: true }, 
    { locationId: 2, day: 'Wednesday', dayValue: 3, dayStart: '9:00am', dayEnd: '8:00pm', open: true, working: true }, 
    { locationId: 2, day: 'Thursday', dayValue: 4, dayStart: '9:00am', dayEnd: '8:00pm', open: true, working: true }, 
    { locationId: 2, day: 'Friday', dayValue: 5, dayStart: '9:00am', dayEnd: '8:00pm', open: true, working: true }, 
    { locationId: 2, day: 'Saturday', dayValue: 6, dayStart: '9:00am', dayEnd: '8:00pm', open: true, working: true } 
], 

locations: any[] = [ 
    { id: 1, name: 'Location 1', }, 
    { id: 2, name: 'Location 2', } 
]; 

location: number = 1; 

答えて

3

使用==代わりに===、O代わりに[value]の代わりに[ngValue]を使用してください。 [value]

、そう表現をするために評価される 1または2に、locationプロパティに格納された値は、文字列「1」または「2」で、あなたはそれを比較、===を使用して偽です。直接入力のHtmlElementプロパティへ

Working plunkr

+0

を実装する方法として、 '[値]'格納データを知らなかったの違いを理解するために「『myinput』」=あなたは[名前]を行う場合にも、同じである

です文字列、よく知っている – locnguyen

関連する問題