gpt4 book ai didi

javascript - 上下文菜单动态创建?

转载 作者:行者123 更新时间:2023-11-30 11:30:24 25 4
gpt4 key购买 nike

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

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

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

替换“警报成本”和“警报玩家金币”。

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

最佳答案

你可以这样做......将这段代码添加到演示 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);
}

然后用 $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]
];

瞧,你很高兴。这是工作代码笔示例。 Working example https://codepen.io/anon/pen/jGBJMY?editors=1010

关于javascript - 上下文菜单动态创建?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46448002/

25 4 0