如何在angularjs中为同一资源选择不同的路径?

时间:2016-09-07 10:01:45

标签: angularjs url-routing

我正在编写一个需要通过三个不同路径访问资源restparameter的客户端。路径为resource/:resourceId/restparameterrestparameter/:restparameterId/restparameterrestmethod/:restmethodId/restparamter。我尝试使用$location来做到这一点,但它不起作用。例如,当我尝试通过path:http://localhost:8085/output/#/restmethod/54/restparameter访问rest参数时,客户端只调用:GET http://localhost:8085/WSAT/multirestparameterManager/restparameter/restparameter,显然会回复404 Not Found。我猜它不会匹配,如果,因为它甚至没有尝试调用GET http://localhost:8085/WSAT/multirestparameterManager/restmethod/restparameter我的控制器是这样的:

WSAT.controller('restparameterCtrl',['$scope','$resource','$location','$route','$routeParams','popupService', 'restparameterFactory',
    function($scope,$resource, $location, $route,$routeParams,popupService, restparameterFactory, $window)
    {   $scope.$location = {};
        var result = $location.url();

        if(result.includes("restparameter")){var Restparameter = $resource('http://localhost\\:8085/WSAT/multirestparameterManager/restparameter/:restparameterId/restparameter/');  
        $scope.restparameters = Restparameter.get({restparameterId: $routeParams.restparameterId});}

        else if(result.includes("resource")){var Restparameter = $resource('http://localhost\\:8085/WSAT/multirestparameterManager/resource/:resourceId/restparameter/');  
        $scope.restparameters = Restparameter.get({resourceId: $routeParams.resourceId});}

        else if(result.includes("restmethod")){var Restparameter = $resource('http://localhost\\:8085/WSAT/multirestparameterManager/restmethod/:restmethodId/restparameter/');  
        $scope.restparameters = Restparameter.get({restmethodId: $routeParams.restmethodId});}

2 个答案:

答案 0 :(得分:0)

创建paramDefaults对象时,您没有传递$resource

如果您希望:restparameterId参数来自传递的对象,那么您应该通过传递默认参数{restparameterId:'@id'}来告诉角度。

其他人也一样。

查看$ngResource了解更多信息。

if (result.includes("restparameter")) {
    var Restparameter = $resource('multirestparameterManager/restparameter/:restparameterId/restparameter/',{restparameterId:'@id'});
    $scope.restparameters = Restparameter.get({ restparameterId: $routeParams.restparameterId });
} else if (result.includes("resource")) {
    var Restparameter = $resource('multirestparameterManager/resource/:resourceId/restparameter/',{resourceId:'@id'});
    $scope.restparameters = Restparameter.get({ resourceId: $routeParams.resourceId });
} else if (result.includes("restmethod")) {
    var Restparameter = $resource('multirestparameterManager/restmethod/:restmethodId/restparameter/',{restmethodId:'@id'});
    $scope.restparameters = Restparameter.get({ restmethodId: $routeParams.restmethodId });
}

答案 1 :(得分:0)

您需要在使用$ resource对象时传递参数。

var URL = 'http://localhost\\:8085/';
var Restparameter = $resource(URL +"WSAT/multirestparameterManager/:masterpath/:Id/restparameter/", "{Id:'@Id'}", {
    });

if (result.includes("restparameter")) {

    $scope.restparameters = Restparameter.get({ Id: $routeParams.restparameterId });

} else if (result.includes("resource")) {

    $scope.restparameters = Restparameter.get({ Id: $routeParams.resourceId });

} else if (result.includes("restmethod")) {

    $scope.restparameters = Restparameter.get({ Id: $routeParams.restmethodId });
}