【问题标题】:Angularjs/ASP.NET: How can I read a json returned from my controller and set it to a variable? [closed]Angularjs/ASP.NET:如何读取从控制器返回的 json 并将其设置为变量? [关闭]
【发布时间】:2016-12-15 22:59:54
【问题描述】:

我正在使用 asp.net mvc 5 并在我的控制器中有一个返回 Json 对象的函数。我还在为我的 javascript 使用 angular,并且我试图声明一个变量,该变量等于从读取 Json 文件返回的文本。我是编码新手,任何帮助将不胜感激。

【问题讨论】:

  • 你是如何在你的 mvc 控制器中调用方法的?你想在哪里设置值?在您的角度控制器范围内?

标签: javascript asp.net angularjs json asp.net-mvc


【解决方案1】:

我在 mvc 中这样做的方式是我有这样的 js 服务:

angular.module('Something')
.controller('SomethingController', ['$scope', 'SomethingService', 'Notification', 'Enum', ....

比从服务调用你的cs控制器:

        somethingService.getSomthing().then(function (data) {
            $scope.value = data.Somthing;
        });

服务:

angular.module('Something')
.factory('SomethingService', ['$http', 'Notification', function ($http, Notification) {
return {    getSomething: function () {
            var promise = $http.post('../../Area/Something/GetSomething')
                .then(function (response) {
                    return response.data;
                }, function (error) {
                    Notification.error("We encountered an error while retrieving Something", error);
                });
            return promise;
        }
    };
}
]);

服务会调用cs控制器

【讨论】:

  • 谢谢,这个解决方案有效
猜你喜欢
  • 2022-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多