由角调用C#web服务的使用$资源组控制器变量

由角调用C#web服务的使用$资源组控制器变量

问题描述:

我有使用的是从正在添加服务JSON数据有问题。
后端是用C#和AngularJS前端。

I have a problem with using the JSON data which is comming from a service. The backend is written in c# and the frontend in AngularJS.

我不能够使用对象我想在角控制器。

I am not able to use the object as I want in the Angular controller.

所以,这里是在C#我的API RESTful服务

So, here is my api restful service in C#

[Route("v1/environment")]
    public IHttpActionResult GetEnvironment()
    {
        var env = new Environment(WebConfigurationManager.AppSettings["Env"]);
        return this.Ok(env);
    }

那么,这里是我的角度服务:

Then, here is my angular service:

module.factory('Environment', [
    '$resource',
    function ($resource) {
        return $resource('/MorpheusApi/v1/environment', {}, {
            getEnv: {method: 'GET', isArray: false}
        });

    }
    ]);

然后用于测试目的,我还创建了这个服务:

Then for testing purpose I created also this service:

 module.factory('Env', [
    'Environment',
    function (Environment) {
        return {
            getEnv: function () {
                var s = Environment.getEnv();
                var e = angular.fromJson(s);
                return e;
            }
        };
    }
    ]);

然后,我会用它的控制器是这样的:

Then I would use it in controller like this:

$scope.envLocal = Env.getEnv();

然后在用户界面,与此

Then in the ui, with this

<p>Environment Local: {{envLocal}}</p>
    <p>Environment Local Env: {{envLocal.Env}}</p>

我收到下面的输出,这是罚款,我想:)

I am getting the following output, which is fine I think :)

Environment Local: {"Env":"testText"}

Environment Local Env: testText

但是,当我现在想要让我们说从控制器到UI提供只是testText,我会做这样的事情:

But when I now want to let's say provide just the testText from the controller to the ui, I would do something like this:

    var result = Env.getEnv();
$scope.envLocal = result.Env;

但这则没有工作。我试了一下还与angular.toJson .. angular.fromJson,但我没有得到任何东西,然后在UI。为什么?我怎样才能在控制器正常使用的对象,例如,如果我想使用它,如果一个或任何..

But this is then not working.. I tried it also with angular.toJson.. angular.fromJson, but I am not getting anything then in the UI.. Why? How can I use the object properly in the controller, for example if I want to use it in an if or whatever..

这甚至可能吗?

您可以写你的工厂这样也让你不需要写两个不同的服务。

You can write your factory like this also so you don't need to write two different services.

module.factory('Environment', ['$resource',
    function ($resource) {
        var api = $resource('/MorpheusApi/:action/:subaction', {}, {
            getEnv: {
                method: 'GET',
                params: {
                   action: "v1",
                   subaction:"environment"
                },
                isArray: false
            }
        });
        return {
            getEnv: function () {
                return api.getEnv().$promise;
            }
        };
    }
]);

之后,你只需要注入该服务到控制器,你可以很容易地调用函数。

After that You just need to inject this service into the controller and you can call function easily.

Environment.getEnv()
                .then(function (response) {
                    //If you wants to perform any logic on response object then you can do here and after that you can assign to scope variable
                    // You can write this logic in service too
                    $scope.envLocal = response;
                });

您可以拨打服务刚刚我说的上方,$资源正在恢复答应你的范围对象将设置成功API和不需要调用 $范围的呼吁。$适用()在这里。

You can call the service just I stated above so as $resource is returning promise your scope object will set on successfully call of api and no need to call $scope.$apply() here.

假设你有两个不同的像

/ MorpheusApi / V1 /环境

/MorpheusApi/v1/environment

/ MorpheusApi / V2 /环境

/MorpheusApi/v2/environment

您可以配置两条路线是这样调用该函数根据自己的需要。

You can configure two routes like this call the function as per your need.

module.factory('Environment', ['$resource',
        function ($resource) {
            var api = $resource('/MorpheusApi/:action/:subaction', {}, {
                getEnv: {
                    method: 'GET',
                    params: {
                       action: "v1",
                       subaction:"environment"
                    },
                    isArray: false
                },
                getEnvV2: {
                    method: 'GET',
                    params: {
                       action: "v2",
                       subaction:"environment"
                    },
                    isArray: false
                }
            });
            return {
                getEnv: function () {
                    return api.getEnv().$promise;
                },
                getEnvV2: function () {
                    return api.getEnvV2().$promise;
                }
            };
        }
    ]);

有关CONFIGRATION的更多信息,并了解你可以看到这篇 CONFIGRATION的说明及$资源工作

For more information of configration and understanding you can read this Explanation of configration and working of $resource