如何创建将参数传递给Rest Web Service的控制器 [英] How do I create a controller that passes a parameter to a rest web service

查看:54
本文介绍了如何创建将参数传递给Rest Web Service的控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

myApp.controller('consumeProduct', function($scope, $http, $log) {    
        $http({
            method: 'GET',
            url: 'http://localhost:8080/Products'
        }).then(function(response) {  
            $scope.products = response.data;
            $log.info(response);
        });
    });

我上面有一个控制器,可以使用我的其余api并返回所有产品.现在,我想创建另一个接受参数(字符串)的控制器,并尝试使用此参数并使用此Web服务(' http://localhost:8080/Products/ProductName/parameter '.根据参数是什么,服务应返回特定的产品.我该怎么做?试图创建一个服务javascript文件来消耗我所有的剩余api资源.

I have a controller above that consumes and returns all products from my rest api. Now I would like to create another controller that takes a parameter(a string) and try to use this parameter and consume this web service('http://localhost:8080/Products/ProductName/parameter'. Based on what the parameter is, the service should return a specific product. How would I be able to do this? I'm trying to create a services javascript file to consume all of my rest api resources. Thanks.

推荐答案

如sabithpocker所建议,控制器仅应用于更新视图.您可能要对$ http请求使用服务或工厂.下面是一个可能的实现示例:

As suggested by sabithpocker, Controllers should only be used to update your view. You may want to use a Service or a Factory for your $http requests. Below is an example of a possible implementation:

myApp.factory('ProductFactory', function($http, $log) {

    var ProductFactory = {};

    ProductFactory.getProduct = function(productName) {
        return $http({
                method: 'GET',
                url: 'http://localhost:8080/Products/ProductName/' + productName
            })
            .then(function(response) {
                return response.data;
            })
            .catch($log.err);
    }

    return ProductFactory;
});

现在您需要将上述工厂注入到Controller中:

Now you need to inject the above factory into your Controller:

myApp.controller('ProductCtrl', function($scope, $log, ProductFactory) {
    ProductFactory.getProduct('apple')
        .then(function(returnedProduct) {
            $scope.product = returnedProduct;
        })
        .catch($log.err);
});

如果将Factory或Service用于$ http进程,则可以在需要时(以及其他Controller)随意重用它们.

If you use a Factory or Service for your $http processes, then you may reuse them at will whenever needed (and for other Controllers as well).

例如,您可能有另一个视图,只需单击一个按钮即可在其中获取产品信息.因此,重复使用Factory,您可能会拥有另一个控制器,如下所示:

For example you may have another view where you want to obtain product information just when clicking on a button. So reusing your Factory you may have a second controller as follows:

myApp.controller('ProductListCtrl', function($scope, $log, ProductFactory) {
    $scope.productClick = function(productName) {
        ProductFactory.getProduct(productName)
            .then(function(returnedProduct) {
                $scope.product = returnedProduct;
            })
            .catch($log.err);
    }
})

在您的html中:

<td><button ng-click="productClick(product.name)">Show product</button></td>

我希望这会有所帮助.

这篇关于如何创建将参数传递给Rest Web Service的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆