【问题标题】:Context Menu creating Dynamically?动态创建上下文菜单?
【发布时间】:2017-09-27 12:33:57
【问题描述】:

我点击了以下链接: https://codepen.io/templarian/pen/VLKZLB

但是在这里单击更多选项时,我必须获得动态填充的名称,例如

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

替换“Alert Cost”和“Alert Player Gold”。

我尝试过,但我无法进入动态循环。

【问题讨论】:

    标签: javascript jquery css angularjs


    【解决方案1】:

    你可以这样做......在演示控制器内的函数中添加这段代码。

    $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 替换旧的 Alert Cost" 和 "Alert Player Gold" 数组。

    $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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-06
      • 1970-01-01
      • 1970-01-01
      • 2016-09-07
      • 2020-10-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多