Scope-specific dynamic variable name

When attempting to create a dynamic variable name within an AngularJS foreach loop, I encountered the following issue:

angular.forEach($scope.posts, function (item) {
    // counter increment
    counter++;

    var idPage = 'feed' + counter;

    FeedService.parseFeed(url).then(function(res) {
        $scope.window[idPage] = res.data.responseData.feed.entries;
    });

});

Unfortunately, this code is not functioning as expected and triggers the error message:

Cannot set property 'feed1' of undefined

Could someone please provide guidance on the correct syntax for achieving this task?

Answer №1

Is there a window property in your $scope object? If so, follow Maxim's solution. Otherwise, give this a try:

angular.forEach($scope.posts, function (item) {
    // increment counter
    counter++;

    var idPage = 'feed' + counter;

    FeedService.parseFeed(url).then(function(res) {
        $scope[idPage] = res.data.responseData.feed.entries;
    });

});

Update:

Here is a functional plunkr link: http://plnkr.co/edit/eVLJKIspLwKPCYAg7L8w?p=preview

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

Adjust the position of the IMG to the left side

Struggling with some code and need some assistance: <script> function run() { document.getElementById("srt").value = document.getElementById("Ultra").value; } </script> <script> function ru ...

Troubleshooting: Configuring dynamic minimum time for <input type='time'> in AngularJS not functioning as expected

Can someone help me with setting the minimum time dynamically to the current time? For some reason, it's not working and I'm not getting any error messages. Where did I make a mistake? $scope.mintime = new Date(); <label class="item item-in ...

Managing various dropdown select options in pure JavaScript

Consider the select element provided below: <select multiple="multiple"> <option value="car">car</option> <option value="scooter">scooter</option> <option value="bus">bus</option> </select> I ...

Issue with modal dialog not triggering onshow event after postback

In my application, I have a Bootstrap modal dialog that is used to display data when the user clicks on "Edit" in a jQuery data table. The modal contains Cancel and Submit buttons. Everything works correctly when I open the modal, click Cancel, select ano ...

What could be causing my Chrome extension to function on Mac but not on a PC?

I created a basic Chrome extension that includes a background page with the following code: <script type="text/javascript> chrome.tabs.onDetached.addListener(function(tabId, info){ var id = tabId; chrome.tabs.get(id, function(tab) { ...

Customizing the size of the selectInput widget in R/Shiny

I'm trying to adjust the size of the selectInput() widget in shiny using selectize.js. I've attempted to tweak various attributes listed on this page (https://github.com/selectize/selectize.js/blob/master/dist/css/selectize.css) but I can't ...

My stored variable in the php session is not being recalled properly

Currently, my PHP script is generating new files based on user input. I want all documents to be created using the initial input to ensure consistency. In the first PHP script, I set the variable as follows: session_start(); $_SESSION["FirstName"] = $_POS ...

Error: The specified module could not be found: Could not resolve 'react-hot-loader/webpack'

After constructing my React project, I encountered the error shown below. How can I resolve this issue? I am utilizing the latest version of react-hot-loader. I have attached a screenshot of the error here ...

Real-time monitoring within a callback function in Angular 5

I need to ensure that a specific callback is executed only after receiving a response, starting from the line this.groupDefaultExpanded = -1; onwards. loadLoginDetails() { this.derivativeSpecService.getDerivativeDetails().subscribe( res => ...

Combining Three Functions into One

Is it possible to merge these 3 functions together for a seamless data stream display in individual divs? How should I approach this? The AJAX request does not retrieve the users' first, middle, or last names under response.users but rather response. ...

Prefixes for logging - Consider using the not-singleton technique or another approach

I am currently developing a logging helper for Node.JS that includes several exported functions such as error and warn. For instance, I have two other scripts called test1 and test2 which make use of this "module". When initializing my logging module us ...

Loop through a JSON object to dynamically update the value of a specific key

I have a JSON object with keys and values, where some of the values are empty strings. I want to replace those empty values with the corresponding key name. However, when trying to get the value of a key within the loop, it returns undefined. JSON: "Forg ...

Having trouble with error handling in Js Node Selenium Try-Catch? Let's troubleshoot

My goal is to simulate the action of clicking on an element that is displayed after a specific iteration of document.readyState = 'complete'. To overcome the readyState issue, I considered using try-catch with the executeScript command to keep re ...

I'm having issues with Node.js, Express, and SQLite3. Whenever I try to use a service for the database query

While using a sqlite3 database for a node.js express project, I encountered an issue. When I follow the tutorial and return the request as shown in router.js, it successfully retrieves all the items. However, when I created a service to get the sql from th ...

What could be causing html-webpack-inline-source-plugin to prevent the page from refreshing after editing CSS files?

Currently, I am utilizing a plugin that embeds all CSS within the final HTML file. This method prevents the application from refreshing in development mode. Whenever I make edits to the CSS, the build process completes normally, but the styles do not updat ...

Is it possible to make multiple AJAX requests at the same time using AngularJS

I am looking to simultaneously send multiple AJAX requests. Here is the JS code I have: <a class='btn btn-success' ng-click='getDataajax()'>Re Check</a> app.controller('customersCrtl', function($scope, $http, $ti ...

I'm having trouble getting my application to output to the console. What could be the issue?

In my cr-route.js file, I have a function that ensures the user is authenticated before displaying their name. module.exports = function (app, passport) { // ===================================== // HOME PAGE (with login links) ======== // == ...

Are Angular HTTP observables distinct in their behavior in comparison to standard observables?

While experimenting with Angular routing, I decided to utilize an in-memory database to retrieve information about the heroes. The original StackBlitz project can be found here. If you go to the heroes tab, select a hero, and modify their name, you' ...

What is the best way to enable and disable the edit-in-place feature using jQuery?

Recently, I decided to experiment with the jQuery In Place Editor but I've come across an issue I can't seem to solve. Below is the code snippet that I have been working on. In my implementation, I am utilizing the jQuery toggle method to enable ...

What could be causing the WebdriverIO browser object to unexpectedly exit my loops?

Each time I try to invoke a method using WebDriverIO's browser object, it abruptly escapes from the current loop I'm in. Upon inspection with the debugger, it is evident that no other code within the loop is being executed. Despite the fact that ...