$範圍我有四個值與名稱:角 - 字符串連接到環
$scope.restaurant.restaurantIsTop
$scope.restaurant.restaurantIsRecommended
$scope.restaurant.restaurantIsNew
$scope.restaurant.restaurantIsPromoted
他們每個人都可以是0或1。我想檢查它們的值是否等於1,將其更改爲true,否則將爲false。
我可以用這個方法四個變量做到這一點:
if ($scope.restaurant.restaurantIsTop == 1 ?
$scope.restaurant.restaurantIsTop = true :
$scope.restaurant.restaurantIsTop = false);
if ($scope.restaurant.restaurantIsNew == 1 ?
$scope.restaurant.restaurantIsNew = true :
$scope.restaurant.restaurantIsNew =false);
if ($scope.restaurant.restaurantIsRecommended == 1 ?
$scope.restaurant.restaurantIsRecommended = true :
$scope.restaurant.restaurantIsRecommended =false);
if ($scope.restaurant.restaurantIsPromoted == 1 ?
$scope.restaurant.restaurantIsPromoted = true :
$scope.restaurant.restaurantIsPromoted =false);
當然它的工作原理,但它並不適用於多個值有效的方式。所以我創建了一個數組:
var varietyArray = ["restaurantIsTop" ,
"restaurantIsRecommended",
"restaurantIsNew",
"restaurantIsPromoted"
];
angular.forEach (varietyArray, function (val) {
console.log($scope.restaurant.val);
})
我想在上面的循環中檢查四個變量。任何建議?
編輯:
我想向他們展示在角材質複選框:
<div class="col-sm-12">
<div class="md-form-group">
<md-checkbox ng-model="restaurant.restaurantIsTop ">
best
</md-checkbox>
<md-checkbox ng-model="restaurant.restaurantIsRecommended">
suggested
</md-checkbox>
<md-checkbox ng-model="restaurant.restaurantIsNew">
new
</md-checkbox>
<md-checkbox ng-model="restaurant.restaurantIsPromoted">
promote
</md-checkbox>
</div>
</div>
'0'和'1'默認爲'falsy'和'truthy'。如果你對同一個變量進行修改,它會改變它的含義。你想在視圖上顯示真或假?或者它只是用於內部計算? –
如果它只有4個這樣的值,If Else是完美的。如果可能,請避免使用三元運算符。以下是鏈接。 http://stackoverflow.com/a/12022491/1907391 –
此外0是錯誤的,最終是真實的。 –