Utilizing a dictionary within an Angular controller

Currently, I am working on a complex process that involves Angular and MVC controllers interacting with a REST API to retrieve configuration items. The challenge lies in transforming this data into a usable format within the Angular controller for various tasks such as displaying information in grids, updating values, and sending changes back to the API.

In my setup, the Angular controller fetches data from the MVC controller, which then queries the REST API to obtain a list of configuration items. This list is converted into a dictionary for easy lookup based on keys. However, I'm facing difficulties in formatting the data correctly within the Angular controller using pipes.

Below are snippets of my code:

Angular Controller:

app.controller("SEFlexHomeController", ["$scope", "$http", "$modal", "$log", "$element", "$rootScope", "AlertsService", "AuthService", "SEApplicationService", function ($scope, $http, $modal, $log, $element, $rootScope, AlertsService, AuthService, SEApplicationService) {
        $rootScope.closeAlert = AlertsService.closeAlert;
        $scope.isDataLoading = false;
        $scope.AuthService = AuthService;
        $scope.configvalues = angular.fromJson(SEApplicationService.getCloudConfigParams());
    }
]);

Angular Service:

app.factory("SEApplicationService", ["$log", "$http", "$timeout", function($log, $http, $timeout) {

    var appService = {};    

    appService.getCloudConfigParams = function () {
        return $http.get("/SEFlex/SEFlexAdmin/GetCloudConfigValues");
    }

    return appService;
}]);

MVC Controller:

public ActionResult GetCloudConfigValues()
{
    try
    {
        var helper = new ApplicationServiceHelper();
        var dictionary = helper.GetCloudConfigValues().ToList().ToDictionary(item => item.ConfigKey, item => item.ConfigValue);

        var returnData = JsonConvert.SerializeObject(dictionary);

        return Json(new
        {
            success = true,
            data = returnData
        }, JsonRequestBehavior.AllowGet);
    }
    catch (Exception exception)
    {
        return Json(new
        {
            success = false,
            errors = new[] { exception.Message }
        });
    }
}

I need assistance in ensuring the data transmission between the MVC and Angular controllers is correct so that I can access the configuration values in Angular using $scope.configvalues["keyName"].

Answer №1

After making a $http.get() request, you will receive a promise. It is advisable to update the

$scope.configvalues = angular.fromJson(SEApplicationService.getCloudConfigParams());

with

SEApplicationService.getCloudConfigParams().then(function(config) {
    $scope.configvalues = config;
});

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

How does React determine if a component is a class component or a functional component?

Within my React application, I have successfully developed both a class component and a functional component. However, I am curious about how React is able to distinguish between the two when calling them. Can you shed some light on this distinction? This ...

Conceal the Addon Tab across all pages in Storybook

Is there a way to globally hide an Addon Tab without disabling the addon itself? Specifically, I am using version 5.3.18 of Storybook with React and I want to hide the tab panel from the addon "styled-component theme". I apologize for the basic question, ...

What is the best way to extract value from a JSON object with jQuery?

How can I retrieve the value of 'FRI' from the JSON feed returned by an AJAX call using jQuery? $.ajax({ url: query, type: "GET", dataType: "json" success: function(data) { var day = // extract data value from JSON ...

Bring in JavaScript files on a global scale in Vue.js 3

Hello, I am facing an issue while trying to import jQuery and THREEJS globally into my Vue.js cli project. Whenever I attempt to import them into my .vue file (like home.vue), I encounter the following error: 'import' and 'export' ma ...

Ways to launch the React Material date picker while hiding the date input field

I am working with the react material (MUI-X) date picker and my specific requirement is to have the date picker open on button click without displaying the date text field. I simply want to show the calendar and allow users to select a date. I have explore ...

Deconstructing JSON in C#

When I receive a JSON response from a RESTful API, parsing it is usually straightforward if I know the keys. For example, creating a User class like this: [DataContract] public class User { [DataMember] public string id { get; set; } [DataMem ...

A proven method for distinguishing between desktop and mobile browsers

Similar Question: Exploring Browser Detection Methods in Javascript I am interested in finding an efficient way to differentiate between desktop and mobile browsers, either using JavaScript or PHP. if (desktop browser) { do x; } else { // mobi ...

How much does it typically cost to implement external libraries into a project?

Here are some of the most commonly used front-end web development libraries: jquery-min.js (95.9 kB) angular.min.js (108.0 kB) bootstrap.min.css (113.5 kB) bootstrap-theme.min.css (19.8 kB) bootstrap-fonts (185.7 kB) bootstrap.min.js (35.6 kB) All in al ...

Issue with delayed chaining of class addition and removal in JQuery not functioning as expected

Here is the code snippet: $(document).ready(function () { $('.current-visitors').delay(3000).queue(function () { $(this).addClass('show').delay(1000).queue(function () { $(this).removeClass('sho ...

Browsing an array of objects to extract targeted information

I'm in the process of developing a node express angular tool for analyzing Twitter statuses and I am facing the challenge of extracting text from it and combining it into a single long string for future use. This is how I am attempting to retrieve us ...

What could be causing my Discord bot to remain offline even when I run the command node main.js?

After successfully installing node.js, I proceeded to type the command 'npm init' in the command prompt and then installed discord.js. However, upon installation of discord.js, a 'node_modules' folder was not added inside the project, w ...

Assign a unique HTML attribute to a child element within a Material-UI component

Currently, I am trying to include the custom HTML5 attribute "data-metrics" in the span element within the ListItemText Material UI component. However, I am facing some difficulty achieving this as per the specifications outlined in the Component API Docum ...

What sets apart the $ and $() functions in jQuery?

After reviewing the jQuery API, I noticed that $() represents a collection of matched elements. However, I am curious about the significance of $. An example from the imagesLoaded library is provided below. if ( $ ) { $.fn.imagesLoaded = function( opt ...

Is there a way to minimize the use of .value in Vue 3?

After recently diving into Vue 3, I find myself struggling to grasp some key concepts of the composition API. My current challenge involves converting a library from Vue 2 to Vue 3, where a reactive property named layout is being passed down to child compo ...

What is the best method for adjusting the text size within a doughnut chart using react-chartjs-2?

Is there a way to adjust the text size within the doughnut chart using react-chartjs-2? I find that the center text appears too small. https://i.stack.imgur.com/QsI0V.png import React, {Fragment} from 'react'; import Chart from 'chart.js&a ...

`The Art of Binding() with Objects Created on the Fly`

Currently facing challenges with rebinding something using a dynamically created object from prepend. Despite trying several methods, I am only able to unbind. Seeking assistance. $(document).ready(function(){ $(".newdir").click(function(){ $(".d-exp ...

Seeking assistance with changing the pages

Looking for assistance with troubleshooting my function. All variables are accounted for. var swith = document.getElementById("sub"); var one = document.getElementById("rule"); var two = document.getElementById("cool"); var three = document.getElementByI ...

Using arrays in Three.js for material instead of MeshFaceMaterial: a guide

I'm starting out with three.js as a beginner. I've run into some issues with MeshFaceMaterial. If anyone has any advice or solutions, I would greatly appreciate it. Thank you in advance! ...

How can I properly choose distinct values for an individual attribute from a JavaScript array containing objects?

Let's imagine a scenario with an array of JavaScript objects like this: var data = [ {category : "root", type: "qqqqq", value1: "aaaaa", value2: "zzzzz"}, {category : "root", type: "qqqqq", value1: "aaaaa", value2: "xxxxx"}, {category : " ...

Tips for deactivating dropdown values based on the selection of another dropdown

There are two dropdown menus known as minimum and maximum, each containing numbers from 1 to 25. If a value is selected from the minimum dropdown (e.g. 4), the maximum dropdown should only allow values that are equal to or greater than the selected value ...