Can we create a dynamic Context Menu?

I visited the following link: https://codepen.io/templarian/pen/VLKZLB

After clicking on More Options, I need to display dynamically populated names like

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

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

I attempted to achieve this through dynamic looping but was unsuccessful.

Answer №1

If you want to achieve this functionality, simply insert the following code inside the function within the 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);
  }

Next, replace the existing 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]
  ];

Once that's done, you're all set to go. Feel free to check out this live example on CodePen: Working example

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips on maintaining the Parent menu in a hovered state when the mouse is over the child menu within a Dropdown Menu

I have been working on creating a dropdown menu that functions correctly. However, I am facing an issue where the top menu, when hovered, turns white, but as soon as I move down to the submenus, the top menu reverts back to its original color. Is there a ...

Guidance that utilizes the scope of a specific instance

I have successfully created a d3.js directive, but I am facing an issue when resizing the window. The values of my first directive seem to be taking on the values of my second directive. How can I separate the two in order to resize them correctly? (both ...

Is there a way to retrieve all the selected values from multiple select inputs when a button is clicked in React?

I am currently working on developing a questionnaire where the select inputs' values are collected and averaged to generate a final score. Although it may sound simple, I'm facing difficulties in retrieving these values. At this point, my primary ...

Maintain Vue Router Query Parameters Across Parent Components

In my application, I have a component named Foo, which serves as the template for a route called app/foo. Within this component, there are child components that also act as templates for routes such as app/foo/bar and app/foo/baz. I've implemented a ...

A step-by-step guide on accessing an API to check the status updates of ongoing API calls

I'm facing an issue with making two API calls concurrently. The goal is to have call X executed first, while Y should run in parallel and keep calling itself recursively until the X API resolves. Both calls should be initiated immediately upon clickin ...

I have created a Joomla Template that incorporates my own CSS through JavaScript. Is there a method to include a distinct version tag to my custom CSS file, such as custom.css?20180101?

My usual method involves adding a unique tag to the css path in the html section, but my template is incorporating custom CSS through Javascript: if (is_file(T3_TEMPLATE_PATH . '/css/custom.css')) { $this->addStyleSheet(T3_TEMPLATE_URL . &apo ...

What could be the reason behind the disruption in this JavaScript associative array?

I'm facing an issue with my associative array, which I have confirmed through console.log to be initially: this.RegionsChecked = {"US":true,"APAC":true,"Canada":true,"France":true,"Germany":true,"India":true,"Japan":true,"LATAM":true,"MEA":true,"UK": ...

Implementing Formik in React for automatic updates to a Material-UI TextField when blurred

Presently, I am developing a dynamic table where users can simultaneously modify multiple user details in bulk (Refer to the Image). The implementation involves utilizing Material-UI's <TextField/> component along with Formik for managing form s ...

Mastering the syntax of Babel in Node.js

Hey there! I've encountered a syntax issue while migrating to babel. The problem lies in importing an unnamed module. In traditional Node.js default import, it's possible to import without specifying the module name and passing in the app. Howeve ...

Angular URL parameter limitations

In my previous experience with Asp.net MVC, I found route constraints to be extremely helpful. Currently, I am working on a Angular JS SPA project that involves client-side routing. I am interested in adding parameter constraints to my client-side routes, ...

Assigning controller variables within an ajax request

I'm new to AngularJS and I have a question about controller attributes. I've created an attribute called 'anuncio', which contains an array of objects as shown below: var anuncioModule = angular.module('anuncioModule',[]); a ...

Tips for adjusting line placement on a leaflet map

My leaflet map displays two lines, but sometimes they appear identical, causing the map to show only one line due to overlap. To address this issue, I am attempting to shift one of the lines slightly so that both are visible on the map. One method I cons ...

Reactjs encountering issues with CSS loading correctly

Currently, I am working with reactjs and utilizing the Nextjs framework. All my "css, js, images" are stored in the "public" folder and have been included in "_app.js". However, whenever I attempt to load the "Main page" in a browser, the page does not d ...

How come my uploaded Excel Javascript add-on opens in an external browser instead of the task pane?

Note: It has come to my attention that I must save the taskpane.html file on my local drive before it opens in an external browser. This detail slipped my notice last week. I am currently developing a Javascript, or rather Typescript, API add-in for Excel ...

What is the purpose of the JSON.stringify() function converting special characters?

I attempted the following code snippet: console.log(JSON.stringify({ test: "\u30FCabc" })); The result is as follows: '{"test":"ーabc"}' We are aware that primarily, the JSON.stringify() method converts a Jav ...

Plane is constantly cloaked in darkness

I'm having trouble adding a texture to my plane that repeats both horizontally and vertically. Every time I try to apply the texture, it shows up as black. I've attempted to add some lights to the scene, but the issue persists without any errors. ...

Getting the value from a .sh (Shell Script) file in React: How to do it?

There is a .sh file that contains the following code: echo "Hello" This code produces the output: Hello The question at hand is: I am trying to extract the output from the .sh file and integrate it into my React application. After exploring various ...

Using TypeScript, pass an image as a prop in a Styled Component

I am facing an issue with the code below that is supposed to display the "NoBillsLaptopPNG.src" image on the screen, but for some reason, the image is not showing up. The images are being imported correctly, so I'm unsure why the image is not appeari ...

Issue with Angular material's md-form-field functionality not functioning properly

I am currently using @angular/material version 2.0.0 beta.10 Within the appModule, I have imported MdFormFieldModule and MdInputModule. This is how my HTML structure looks: <md-form-field> <input type="text" mdInput placeHolder="Hi"> </ ...

What is the method for implementing a Redirect in React Router?

I'm new to React and facing a challenge. My goal is to implement a conditional Redirect, and I have created a function called errorCapture for this purpose. The function should trigger a Redirect based on a specific condition. I have included the ret ...