2015-11-20 46 views
0

我試圖根據給定的數組結構更改顯示結果。我認爲我可以用ng-switch做到這一點,但是找不到解決問題的方法。AngularJS - ngSwitch與多維數組

我有這樣的JSON:

0: {size: 1, room: 1, id: 1, name: "",…} 
huesped: {id: 1, name: "Pablo", lastname: "Bertran", passport: "33025915N", birth: "1987-04-14", country: 10,…} 
id: 1 
name: "" 
room: 1 
size: 1 
stay: {id: 1, booking: 0, room: 1, bed: 1, guest: 1, indate: "2015-11-02", outdate: "2015-12-02", hotel: 1,…} 
1: {size: 1, room: 1, id: 2, name: ""} 
2: {size: 1, room: 1, id: 3, name: ""} 
3: {size: 1, room: 1, id: 4, name: ""} 
4: {size: 1, room: 1, id: 5, name: ""} 
5: {size: 1, room: 1, id: 6, name: ""} 
6: {size: 2, room: 2, id: 7, name: "1"} 

的事是。如果聲明'huesped'的位置,我需要一個場景。 如果stay.booking> 0,方案B. 如果沒有停留宣佈,情景C.

我試過這段代碼: (假設JSON是在$ scope.beds)

    <div ng-switch on="bed"> 
         <div ng-switch-when="bed.huesped"> 
          <div class="md-list-item-text" ng-click="accion(bed.guest.id, 2)" style="background-color: rgba(0,0,255,0.7);"> 
           <h3>Cama - {{::bed.id}}</h3> 
           <h4>{{ bed.huesped.name }} {{ bed.huesped.lastname }}</h4> 
          </div> 
         </div> 
         <div ng-switch-when="bed.stay.booking"> 
          <div class="md-list-item-text" ng-click="accion(bed.booking.id, 3)" style="background-color: rgba(255,0,0,0.7);"> 
           <h3>Cama - {{::bed.id}}</h3> 
           <h4>{{ bed.booking.name }}</h4> 
          </div> 
         </div> 
         <div ng-switch-default> 
          <div class="md-list-item-text" ng-click="accion(bed.id, 1)" style="background-color: rgba(0,255,0,0.7);"> 
           <h3>Cama - {{::bed.id}}</h3> 
           <h4>{{::bed.size}} Plaza(s)</h4> 
          </div> 
         </div> 
        </div> 

我知道什麼是錯的。不知道代碼中是否有細節,或者如果ng-switch無法做到我需要的。 希望你能幫助我。

回答

1

https://docs.angularjs.org/api/ng/directive/ngSwitch說:

要知道,來匹配無法表達的屬性值。它們被解釋爲文字串值以匹配。例如,ng-switch-when =「someVal」將與字符串「someVal」匹配,而不是針對表達式$ scope.someVal的值。

ng-if即可輕鬆解決您的問題。