简体   繁体   中英

how to get angularjs routeParams data into input from two different Database tables?

my database has two tables one for "Customers" and another for "Tripsheets". I would like to call the routeParams data of Customers into Tripsheets. I tried something like this, but it does'nt work

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;
     }
   });


  });

});

Since you're making the calls in the same controller TripsheetCtrl1 you will be sharing the same $scope ...so first populate $scope.customers and $scope.whichItem by calling the $http.get and then use those values in your 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;
  });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM