Encountering difficulties resolving my dynamic JSON data when compared to the static JSON data in the ng

I have a listbox and I am binding a list of items from a controller.

$scope.AvailableListItems = [
    [{Id:1,SupplierName: 'john.banks'},
    {Id: 2,SupplierName: 'jim.chevy'}, 
    {Id: 3,SupplierName: 'ralph.stocks'}]
];

This JSON data is hardcoded. It works fine when used with the following HTML:

<select multiple id="availabelist" size="10" style="width:100%" ng-change="OnAvailableChange()" ng-model="SelectedAvailItems" ng-options="i as i.email for i in AvailableListItems[selectFaIndex]| filter:availablequery"></select>

However, when I try to generate the same thing dynamically, it does not work and shows a blank list box. The code snippet for this dynamic generation is as follows:

 var getSuppliers = function () {  
            var tempArray = [];
            var lstsuppliers = CRUDService.getApiOutput(getSuppliersApiRoute);
            lstsuppliers.then(
                function (response) {
                    debugger;
                    $scope.supplierList = response.data;
                    for (var i = 0; i < $scope.supplierList.length; i++) {
                        arr = {};
                        arr["Id"] = $scope.supplierList[i].supplierId;
                        arr["SupplierName"] = $scope.supplierList[i].supplierName;
                        tempArray.push(arr);
                    }
                    $scope.AvailableListItems = tempArray;
                    console.log(JSON.stringify($scope.AvailableListItems));
                },
                function (error) {
                    console.log("Error: " + error);
                });
}

I need help identifying what could be wrong with my code.

Link to Working Plunk Example

Here is how my response data appears based on Claies: https://i.sstatic.net/g8GkP.png

[{"Id":1,"SupplierName":"ACECO PRECISION MANUFACTURING"},{"Id":2,"SupplierName":"Pentagon EMS Corporation"},{"Id":3,"SupplierName":"QUANTUMCLEAN"},{"Id":4,"SupplierName":"MODERN CERAMICS"},{"Id":5,"SupplierName":"NXEDGE INC"}]

Answer №1

After spending hours relentlessly searching, I finally uncovered the solution I was looking for. I combined both hardcoded and dynamic values to console, and noticed a significant difference. https://i.sstatic.net/bYje8.png

I made a modification

$scope.AvailableListItems = tempArray;

to

$scope.AvailableListItems.push(tempArray);

on line 14, and surprisingly, it worked like a charm. The output completely transformed after implementing this change https://i.sstatic.net/UE7pr.png

A big thank you to @Tomas and @Claies for their valuable input.

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

The getElementById method in JavaScript can result in a null return value

Why is null returned by the getElementById method in JavaScript? <html> <head> <title>test_elementObject</title> <script language="JavaScript" type="text/javascript"> <!-- var input1 = document.getElementById ( " ...

Unable to utilize the ng-disabled directive

Although this question may seem redundant, rest assured that I have exhausted all possible solutions. The issue lies in a disabled button using ng-disabled. I have a variable in the $scope that triggers a change in the ng-disabled attribute, but it fails t ...

What are the steps to include a scrollable tooltip in HTML?

Is there a way to implement a scrollable tooltip without using an iframe? Any existing jquery or css scripts that can help achieve this? If so, how can it be done? ...

Scrollable container with jQuery draggable functionality

I am currently working on implementing a draggable list that I want to contain within a scrollable div. $( ".draggable" ).draggable(); Here is the fiddle I have created for this: http://jsfiddle.net/YvJhE/660/ However, the issue I am facing is that the ...

angularsjs state provider with multiple parameters

I am struggling to create a state provider that can handle multiple parameters. Is it possible to capture them as an object or array, or do I have to capture them as a string and then separate them? For example, this is my current provider: .state(' ...

The guard check may not be enough to prevent the object from being null

Hello, I am facing an issue with the Object is possibly "null" error message in my Node.js + Express application. How can I resolve this problem? REST API export const getOrderReport = async ( req: Request<{}, {}, IAuthUser>, res: Resp ...

a method for inserting a space after a certain character, with the exception of when that character is located at the start or end of a line

I've created a regular expression that can modify various patterns like: anything1 * anything2* anything3 anything1* anything2 * anything3 anything1 * anything2 * anything3 anything1*anything2 *anything3 anything1 * anything2 *anything3 anything1*any ...

Variable passing value through google pie chart slices for offset

Using the Google Chart API and AngularJS, I am trying to set slice values through variables instead of hardcoding or using a foreach loop. How can I achieve this? In my code example below, the "selectedRow" variable is being passed as a string rather tha ...

Employing AJAX to send form data to a pair of destinations

Can anyone help me out? I am having trouble using AJAX to submit my form to one location and then to another. Once it's posted to the second location, I want it to send an email with PHP to a specified recipient, but for some reason, it's not wor ...

Synchronous: Establishing a connection to MongoDB once the server has successfully launched

I am curious to understand the interaction between Node.js asynchronous functions and MongoDB. If the server initializes before establishing a connection, and if my templates or Ajax requests rely on database data, will the serving of the HTML fail or will ...

How to stop double-clicking on a button in angularjs?

How can I display a list multiple times when a user clicks on a button in AngularJS? ...

Dealing with Errors When Working with Angular Promises

Currently, I am in the process of mastering promises within Angular. In my code snippet, I have two "GET" requests that I need to execute sequentially. Everything is functioning properly, but I'm unsure about how to handle errors in this scenario. If ...

What is the best way to send an array of objects to a Jade template?

I'm looking to retrieve an array of objects from MongoDB and pass it to the client... Here is an example object: var objeto_img= { name:'name of the file', ...

What is the best way to display data retrieved from a GET request in Angular?

Spending too much time on a development issue is causing me frustration. After extensive research, I find myself stuck at this point. The problem lies in making a GET request from a service which is called by a controller. Below is the code for the servi ...

Is there a way to include e.preventDefault() within an ajax call?

After the user clicks the submit button on my form, the handleSubmit function is triggered. However, I am having trouble calling e.preventDefault() inside my AJAX call due to its asynchronous nature. How can this issue be resolved? class FormRegister ex ...

No longer able to bind events after making changes to SVG elements [using JSFiddle]

I've coded a personalized directive to display tags like `<rect>` and `<circle>` within `<svg>` elements. My shapes model is simply an array of objects with different attributes such as `width`, `height`, and `stroke-width. These s ...

How can nextJS leverage async getInitialProps() method in combination with AWS S3?

I'm currently facing a challenge with executing an s3.getObject() function within an async getInitialProps() method in a nextJS project. I'm struggling to properly format the results so that they can be returned as an object, which is essential f ...

Disabling the ability to edit the rightmost portion of an input number field

I am looking for something similar to this: https://i.stack.imgur.com/ZMoNf.jpg In this case, the % sign will be shown in the input field by default and cannot be changed or removed. The user is only able to modify the number to the left of the % sign. P ...

Having trouble connecting to Docker? The webpage at localhost seems to be unresponsive and is not providing any data. Error: ERR_EMPTY_RESPONSE

Currently in the process of mastering docker, kafka, and python. My objective is to leverage python for extracting data from json files and transmitting them to kafka. The setup seems to be functioning properly, but I'm facing challenges with viewin ...

Setting the base path for a statically exported NextJS app

I am in the process of developing and deploying a React / NextJS application to a Weblogic J2EE server with a specific context. While I have some experience with React, I am relatively new to NextJS. The current steps for building and verifying the app ar ...