2017-09-27 3 views
1

私はリンクの下のこの続き: https://codepen.io/templarian/pen/VLKZLB動的に作成するコンテキストメニュー?

をしかし、ここでより多くのオプションをクリックしたときに、私は

var Obj = [{name:"1st Item",taste:"sweet"},{name:"2nd item",taste:"spicy"}]; 

のようなダイナミックな人口名前は「アラートコスト」と「アラートプレーヤーゴールド」の置き換え取得する必要があります。

私は試みましたが、私は動的なループに入ることに失敗しました。

+0

テキストが記載されたコード段落 – Alburkerk

答えて

3

このようにすることができます....このコードをデモコントローラ内の機能の中に追加します。その後、

$scope.arr = []; 
    for(let x of [{name:"1st Item",taste:"sweet"},{name:"2nd item",taste:"spicy"}]) { 
    let newArr = []; 
    newArr.push(x.name); 
    newArr.push(function ($itemScope) { 
       alert($itemScope.item.cost); 
      }); 
    $scope.arr.push(newArr); 
    } 

と$ scope.arrでアラートコスト」と "アラートプレーヤーゴールド"の古い配列を置き換える。

$scope.menuOptions = [ 
     ['Buy', function ($itemScope) { 
      $scope.player.gold -= $itemScope.item.cost; 
     }], 
     null, 
     ['Sell', function ($itemScope) { 
      $scope.player.gold += $itemScope.item.cost; 
     }, function ($itemScope) { 
      return $itemScope.item.name.match(/Iron/) == null; 
     }], 
     null, 
     ['More...', $scope.arr] 
    ]; 

ほら、行くためにあなたが良い。ここで はcodepenに取り組んでいます例。Working examplehttps://codepen.io/anon/pen/jGBJMY?editors=1010

関連する問題