Angularjs 带:id的$http get

Angularjs 带:id的$http get,angularjs,Angularjs,我正在尝试使用$http服务获取以下url路径 假设我有一根绳子 “/api/users/:id/fullinfo” ,id为5,如何获取此字符串的以下格式: “/api/users/5/fullinfo” ,通过$http.get()方法或$resourceAPI。Code 将此服务包括在您的应用程序中: app.service( "httpi", function( $http ) { return( httpProxy );

我正在尝试使用$http服务获取以下url路径

假设我有一根绳子

“/api/users/:id/fullinfo”

,id为5,如何获取此字符串的以下格式:

“/api/users/5/fullinfo”

,通过$http.get()方法或$resourceAPI。

Code 将此服务包括在您的应用程序中:

    app.service(
        "httpi",
        function( $http ) {
            return( httpProxy );
            // I proxy the $http service and merge the params and data values into
            // the URL before creating the underlying request.
            function httpProxy( config ) {
                config.url = interpolateUrl( config.url, config.params, config.data );
                return( $http( config ) );
            }
            // I move values from the params and data arguments into the URL where
            // there is a match for labels. When the match occurs, the key-value
            // pairs are removed from the parent object and merged into the string
            // value of the URL.
            function interpolateUrl( url, params, data ) {
                // Make sure we have an object to work with - makes the rest of the
                // logic easier.
                params = ( params || {} );
                data = ( data || {} );
                // Strip out the delimiter fluff that is only there for readability
                // of the optional label paths.
                url = url.replace( /(\(\s*|\s*\)|\s*\|\s*)/g, "" );
                // Replace each label in the URL (ex, :userID).
                url = url.replace(
                    /:([a-z]\w*)/gi,
                    function( $0, label ) {
                        // NOTE: Giving "data" precedence over "params".
                        return( popFirstKey( data, params, label ) || "" );
                    }
                );
                // Strip out any repeating slashes (but NOT the http:// version).
                url = url.replace( /(^|[^:])[\/]{2,}/g, "$1/" );
                // Strip out any trailing slash.
                url = url.replace( /\/+$/i, "" );
                return( url );
            }
            // I take 1..N objects and a key and perform a popKey() action on the
            // first object that contains the given key. If other objects in the list
            // also have the key, they are ignored.
            function popFirstKey( object1, object2, objectN, key ) {
                // Convert the arguments list into a true array so we can easily
                // pluck values from either end.
                var objects = Array.prototype.slice.call( arguments );
                // The key will always be the last item in the argument collection.
                var key = objects.pop();
                var object = null;
                // Iterate over the arguments, looking for the first object that
                // contains a reference to the given key.
                while ( object = objects.shift() ) {
                    if ( object.hasOwnProperty( key ) ) {
                        return( popKey( object, key ) );
                    }
                }
            }
            // I delete the key from the given object and return the value.
            function popKey( object, key ) {
                var value = object[ key ];
                delete( object[ key ] );
                return( value );
            }
        }
    );
用法 在包含并注入它之后,您应该能够执行以下操作

httpi({
    method: "post",
    url: "/sample/:id",
    data: {
        id: "1"
    }
});
它将调用带有POST的“/sample/1”,并且不再有数据

来源
运行示例

我想你说的是,你的应用程序中有一个GET API端点,它响应动态路由
'/API/users/:id/fullinfo'
,你正在试图弄清楚如何使用
$http
提供程序发出GET请求,对吗?如果是这种情况,则可以执行以下操作:

// create a service to manage the users resource
app.factory('UsersService', ['$http', function($http) {
    function getById(id) {
        var requestUrl = '/api/users/' + id + '/fullinfo';
        return $http.get(requestUrl).then(function(resp) {
            return resp.data;
        }, function(err) {
            // handle request error generically
        });
    }

    return {
        getById: getById
    };
}]);

// consume the service in a controller like so:
app.controller('YourController', ['$scope', 'UsersService', function($scope, UsersService) {
    var userId = 5; // you might also get this value from state params or some other dynamic way

    UsersService.getById(userId).then(function(userInfo) {
        $scope.userInfo = userInfo;
    });
}]);

您应该能够使用
$interpolate

var getPath = '/api/users/{{id}}/fullinfo';
$scope.id= '5';
$interpolate(getPath)($scope);
或者,您也可以执行以下操作:

var path = '/api/users/' + id + '/fullinfo';
但是插值更好

$interpolate