2016-08-25 12 views
0
$scope.ptArray={m1 : $scope.somevalue1, 
       m2 : $scope.somevalue2, 
       m3 : $scope.somevalue3}; 
$scope.errMsg={m1 : 'msg1', 
       m2 : 'msg2', 
       m3 : 'msg3'}; 
if($scope.ptArray.this==""){ 
      alert($scope.errMsg.this); 
      } 

「this」はここでは機能しません。私が 'this'の代わりにm1、m2、またはm3を使用すると、その変数に対してのみ動作します。あなたはあなたが使用できる辞書にアクセスしたい場合は、このAngularJSの配列インデックス

+0

を試すことはできますか?それは魔法ではない。 – Soviut

+0

私はちょうど私が欲しかったことを伝えようとしていました:) –

答えて

2

あなたはこのような何かを行うことができます。

値として空のStringを持っているすべてのキーを取得:

var empty = []; 
Object.keys($scope.ptArray).forEach((k) => {if(ptArray[k] == ""){empty.push(k);}}); 

は、これらすべてのキーのためのメッセージとアラートを作成します。

var msg = ""; 
empty.forEach((k) => {msg+=$scope.errMsg[k]+"\n"}); 
if(msg.length > 0){ 
    alert(msg); 
} 

、またはすべてを一度に1つのループで実行します。

var msg = "": 
Object.keys($scope.ptArray).forEach((k) => 
     { 
     if(ptArray[k] == ""){ 
      msg += $scope.errMsg[k]+"\n"; 
     } 
     }); 
if(msg.length > 0){ 
    alert(msg); 
} 
0

の代わりに使用する...

strは値 'm1''m2'または 'm3'の文字列です
if($scope.ptArray[str]==""){ 
    alert($scope.errMsg[str]); 
} 

。 しかし、より良い選択肢であることができ、アレイを使用します。インデックスは整数値である

if($scope.ptArray[index]==""){ 
    alert($scope.errMsg[index]); 
} 

thisで何をしたいのか分かりません。

+0

$ scope.errMsg [index]は[object Object]を返します –

0

あなたは「これは」と何を期待し、この

$scope.ptArray={m1 : $scope.somevalue1, 
      m2 : $scope.somevalue2, 
      m3 : $scope.somevalue3}; 
$scope.errMsg={m1 : 'msg1', 
      m2 : 'msg2', 
      m3 : 'msg3'}; 

Object.keys($scope.ptArray).forEach(function(node){ 
      if($scope.ptArray[node]==""){ 
       console.log($scope.errMsg[node]); 
      } 
    }); 
関連する問題