AngularJS - 具有多维数组的 ngSwitch
AngularJS - ngSwitch with multi dimension array
我正在尝试根据给定的数组结构更改显示结果。我以为我可以用ng-switch做到这一点,但找不到解决它的方法。
我有这个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 是否无法执行我需要的操作。
希望你能帮助我。
https://docs.angularjs.org/api/ng/directive/ngSwitch 说:
Be aware that the attribute values to match against cannot be expressions. They are interpreted as literal string values to match against. For example, ng-switch-when="someVal" will match against the string "someVal" not against the value of the expression $scope.someVal.
您的问题很容易通过 ng-if
解决。
我正在尝试根据给定的数组结构更改显示结果。我以为我可以用ng-switch做到这一点,但找不到解决它的方法。
我有这个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 是否无法执行我需要的操作。 希望你能帮助我。
https://docs.angularjs.org/api/ng/directive/ngSwitch 说:
Be aware that the attribute values to match against cannot be expressions. They are interpreted as literal string values to match against. For example, ng-switch-when="someVal" will match against the string "someVal" not against the value of the expression $scope.someVal.
您的问题很容易通过 ng-if
解决。