从角度控制器访问字典

时间:2015-10-08 16:46:46

标签: javascript json angularjs asp.net-mvc

我正在尝试执行以下操作。

Angular控制器调用和MVC控制器GET方法。然后,此方法调用Web上的REST API,该API返回配置项列表。然后我将其转换为字典,以便我可以根据键查找配置值,然后我想将其传递回Angular控制器并将其存储在我可以从许多不同场景访问的变量中,例如显示它们在网格中,更新值,更改和更新它们回到REST API等。我试图设置管道,但我似乎无法在Angular控制器中以可读/可用的格式获取数据。

我的控制器

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());
    }
]);

My 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控制器

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 }
                });
            }
        }        

我可以在MVC控制器中创建字典时确认,字典看起来像.NET字典的预期。我需要做什么才能在传回之前或之后将其转换为Angular,以便我可以以角度访问它

  

$ scope.configvalues [ “的keyName”]

1 个答案:

答案 0 :(得分:0)

$http.get()来电返回a promise。你应该改变

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

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