2016-04-23 2 views
1

角度とIonicを持つサーバーからJSONファイルを読み込みます。Ionic alert - 結果が定義されていないかどうかを確認します。

これは私のコードです:項目について

$scope.showAlert = function(mo,di,mi,don,fr,sa,so) { 
       $ionicPopup.alert({ 
       title: 'Success', 
       content: mo + "<br>" + di + "<br>" + mi + "<br>" + don + "<br>" + fr + "<br>" + sa+ "<br>" + so 
       }).then(function(res) { 
       console.log('Test Alert Box'); 
       }); 
      }; 

<i class="icon ion-ios-clock-outline links" ng-click="showAlert(item.openingHours[0], item.openingHours[1], item.openingHours[2], item.openingHours[3], 
    item.openingHours[4], item.openingHours[5], item.openingHours[6] 
)"></i> 

私の問題は、時には例item.openingHoursの結果は、[6]は未定義であるということです。私は私の警告の中で未定義のテキストを望んでいません。アラートで値が定義されていないかどうかを確認する方法はありますか?

答えて

2

使用条件(三)オペレータ?、定義されている場合、strが定義されていることを確認するためには、brで値を返し、そうでない場合 - 空の文字列:固定

$scope.showAlert = function(mo, di, mi, don, fr, sa, so) { 
    function getStrWithBr(str) { 
    return str ? str + '<br/>' : ''; 
    } 
    var content = 
    getStrWithBr(mo) + 
    getStrWithBr(di) + 
    getStrWithBr(mi) + 
    getStrWithBr(fr) + 
    getStrWithBr(sa) + 
    so || ''; 
... 
+0

、今 'br'は、ときにのみ追加されます値が定義されています。 – alexmac

+0

ありがとう@アレキサンダー! – olivier

関連する問題