【问题标题】:Optional Parameters in angularJS $http getangularJS $http get 中的可选参数
【发布时间】:2015-12-18 23:32:33
【问题描述】:

我使用 Slim 框架构建了一个 API。 我在 Slim 中的一条路线接受可选参数。 有什么方法可以使用 angularjs $http.get 来做到这一点。如何在请求中设置可选参数。

下面是我的代码;

$scope.getRestaurant = function () {
    return $http({
        method: 'get',
        url: "http://api.example.co.uk/web/restaurant/details/" + $scope.id + "/" + $scope.u,
        headers: {'Content-Type': 'application/x-www-form-urlencoded'}
    }).then(function successCallback(response) {
        // this callback will be called asynchronously
        // when the response is available
        return response.data;
    }, function errorCallback(response) {
        // called asynchronously if an error occurs
        // or server returns response with an error status.
        return [];
    });
}

如您所见,我有$scope.id$scope.u。我希望$scope.u 是可选的。目前它总是通过,即使它为空。

【问题讨论】:

    标签: javascript angularjs slim


    【解决方案1】:

    如果它是真实的,只需将其添加到网址中。

    var baseUrl = 'http://api.example.co.uk/web/restaurant/details/' + $scope.id;
    if($scope.u) baseUrl += '/' + $scope.u;
    

    【讨论】:

      【解决方案2】:

      //已编辑

      您可以使用如下所示的三元测试:

       $scope.getRestaurant = function () {
           return $http({
               method: 'get',
               url: "http://api.example.co.uk/web/restaurant/details/" + $scope.id + 
                   ( $scope.u != null ) ? "/"+$scope.u : ""   ,
               headers: {'Content-Type': 'application/x-www-form-urlencoded'}
           }).then(function successCallback(response) {
               // this callback will be called asynchronously
               // when the response is available
               return response.data;
           }, function errorCallback(response) {
               // called asynchronously if an error occurs
               // or server returns response with an error status.
               return [];
           });
       }
      

      或在您返回之前创建您的网址

       $scope.getRestaurant = function () {
      
           var url  = "http://api.example.co.uk/web/restaurant/details/" + $scope.id +
                   ( $scope.u != null ) ? "/"  +$scope.u : "" ;
      
           return $http({
               method: 'get',
               url: url,
               headers: {'Content-Type': 'application/x-www-form-urlencoded'}
           }).then(function successCallback(response) {
               // this callback will be called asynchronously
               // when the response is available
               return response.data;
           }, function errorCallback(response) {
               // called asynchronously if an error occurs
               // or server returns response with an error status.
               return [];
           });
       }
      

      【讨论】:

      • 我需要检查$scope.u 是否为空,您的代码接缝正在检查$scope.id
      猜你喜欢
      • 2015-09-07
      • 2014-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-26
      • 1970-01-01
      相关资源
      最近更新 更多