如何将angularjs routeParams数据转换为来自两个不同数据库表的输入?

时间:2014-06-01 23:34:02

标签: javascript angularjs

我的数据库有两个表格,用于"客户"和#34; Tripsheets"。 我想将客户的routeParams数据称为Tripsheets。 我尝试过类似的东西,但确实有效

HTML

 <div class="row mini-stat" ng-controller="TripsheetCtrl1" >
<div class="col-md-4">
    <div class="input-group m-bot15">
      <span class="input-group-addon btn-white"><i class="fa fa-user"></i></span>
      <input ng-model="tripsheet.tripsheet_num" type="text" class="form-control input-lg"   > 
    </div>
</div>

JS

var urltd = 'http://localhost/tripsheets'; 

  app.factory('tripsheetFactoryd', function ($http) { 
   return { 
   getTripsheetsd: function () { 
   return $http.get(urltd + '/all'); 
    }, 
  addTripsheetd: function (tripsheet) { 
   return $http.post(urltd, tripsheet ); 
    }, 
  deleteTripsheetd: function (tripsheet) { 
   return $http.delete(urltd + '?id=' + tripsheet.id); 
    }, 
  updateTripsheetd: function (tripsheet) { 
   return $http.put(urltd + '?id=' + tripsheet.id, tripsheet); 
    } 
  }; 
 }); 



var url = 'http://localhost/customers'; 

 app.factory('customerFactory', function ($http) { 
  return { 
   getCustomers: function () { 
   return $http.get(url + '/all'); 
  }, 
  addCustomer: function (customer) { 
   return $http.post(url, customer); 
  }, 
  deleteCustomer: function (customer) { 
   return $http.delete(url + '?id=' + customer.id); 
  }, 
 updateCustomer: function (customer) { 
   return $http.put(url + '?id=' + customer.id, customer); 
  } 
}; 


 }); 


   app.controller('TripsheetCtrl1', function ($scope, tripsheetFactoryd, customerFactory, $routeParams) { 
      $scope.tripsheets = []; 


 tripsheetFactoryd.getTripsheetsd().then(function(data){
    $scope.tripsheets = data.data;

    $scope.tripsheet = {
     tripsheet_num: "{{customers[whichItem].name}}"
    };



  $http.get(url + '/all').success(function(data) {
     $scope.customers = data;
     $scope.whichItem = $routeParams.itemId;

   if ($routeParams.itemId > 0) {
     $scope.prevItem = Number($routeParams.itemId)-1;
     } else {
        $scope.prevItem = $scope.customers.length-1;
     }

   if ($routeParams.itemId < $scope.customers.length-1) {
     $scope.nextItem = Number($routeParams.itemId)+1;
     } else {
        $scope.nextItem = 0;
     }
   });


  });

});

1 个答案:

答案 0 :(得分:0)

由于您在同一个控制器TripsheetCtrl1中进行通话,因此您将共享相同的$scope ...因此,首先通过调用$scope.customers$scope.whichItem来填充$http.get然后在getTripsheetsd()中使用这些值。

   app.controller('TripsheetCtrl1', function ($scope, tripsheetFactoryd, customerFactory, $routeParams) { 
      $scope.tripsheets = []; 

   customerFactory.getCustomers().success(function(data) {
     $scope.customers = data;
     $scope.whichItem = $routeParams.itemId;
     $scope.tripsheet = {
       tripsheet_num: $scope.customers[$scope.whichItem].name;
     };

   if ($routeParams.itemId > 0) {
     $scope.prevItem = Number($routeParams.itemId)-1;
     } else {
        $scope.prevItem = $scope.customers.length-1;
     }

   if ($routeParams.itemId < $scope.customers.length-1) {
     $scope.nextItem = Number($routeParams.itemId)+1;
     } else {
        $scope.nextItem = 0;
     }
   });

 tripsheetFactoryd.getTripsheetsd().then(function(data){
    $scope.tripsheets = data.data;
  });
});