Simha Chalam
Simha Chalam

Reputation: 591

Context Menu creating Dynamically?

I followed this below link : https://codepen.io/templarian/pen/VLKZLB

But here when clicking More Option, I have to get the dynamic populated Names like

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

Replace of "Alert Cost" and "Alert Player Gold".

I tried but I am failed to get in dynamic looping.

Upvotes: 1

Views: 91

Answers (1)

Abhishek Singh
Abhishek Singh

Reputation: 1671

You can do like this.... Add this code inside the function inside demo controller.

$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);
  }

And then replace the old array of Alert Cost" and "Alert Player Gold" with $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]
  ];

Voila, you good to go. Here is working codepen example. Working example https://codepen.io/anon/pen/jGBJMY?editors=1010

Upvotes: 3

Related Questions