将ng-model输入日期传递给服务以进行api咨询

时间:2017-06-07 16:02:48

标签: angularjs

我需要将用户选择的日期从我的视图传递到我的服务。

查看:

<div class='input-group date'>
    <input type="date" ng-model="dateLinea" class="form-control"/>
</div>

控制器:

function MainCtrl($scope, consumeApi, $filter) {
     consumeApi.getFullByron().success(function(data){
        $scope.dataFromFactory = data;
 });

工厂:

function consumeApi($http) {

    // Return the object
    return {

        // Create simple method to get data from $http service
        getFullByron : function() {
            return $http({
                url: 'http://localhost/proveedores_api/core/api/proveedores/negocio_linea/negocio_linea_proveedor.php?api_key=ff19974921e713dd4ae3a6592532cd9d&function=negocio_linea_fecha&fecha=' + dateLinea,
                method: 'GET'
            })
        }
    }

}

网址在日期之前结束,但我需要包含从视图传入的日期。

我需要将dateLinea传递给我的服务,因此,当我选择日期时,该日期会被添加到网址以发出请求。

2 个答案:

答案 0 :(得分:0)

看看这是否适合您。

function MainCtrl($scope, consumeApi, $filter) {
//instead of null set this value to the whatever initial page load value needed. 
  $scope.dateLinea = null;

  consumeApi.getFullByron($scope.dateLinea)
   .then(function(data) {
        $scope.dataFromFactory = data;
   })
   .catch(function(err) {
       console.log(err)
   });

  function consumeApi($http) {

    // Return the object
    return {

      // Create simple method to get data from $http service
      getFullByron: function(dateLinea) {
        return $http.get('http://localhost/proveedores_api/core/api/proveedores/negocio_linea/negocio_linea_proveedor.php?api_key=ff19974921e713dd4ae3a6592532cd9d&function=negocio_linea_fecha&fecha=' + dateLinea)
          .then(function(resp) {
          // here you can return the whole response or just data, depending on what you need or other preprocessing.
            return resp.data;
          })
      }
    }

  }

编辑:

function MainCtrl($scope, consumeApi, $filter) {
  //instead of null set this value to the whatever initial page load value needed. 
  $scope.dateLinea = null;

  function exampleGetWrapperFunction() {
    if ($scope.dateLinea) {
      consumeApi.getFullByron($scope.dateLinea)
        .then(function(data) {
          $scope.dataFromFactory = data;
        })
        .catch(function(err) {
          console.log(err)
        });
    }
  }
  exampleGetWrapperFunction();
}

答案 1 :(得分:0)

只需将模型作为参数传递给API,并在服务中使用它......

代码如下

控制器:

function MainCtrl($scope, consumeApi, $filter) {
 consumeApi.getFullByron($scope.dateLinea).success(function(data){
    console.log(data);
    $scope.dataFromFactory = data;
});

在服务中:

function consumeApi($http) {

// Return the object
return {

    // Create simple method to get data from $http service
    getFullByron : function(dateLinea) {
        return $http({
            url: 'http://localhost/proveedores_api/core/api/proveedores/negocio_linea/negocio_linea_proveedor.php?api_key=ff19974921e713dd4ae3a6592532cd9d&function=negocio_linea_fecha&fecha=' + dateLinea,
            method: 'GET'
        })
    }
  }

}