Javascript 如何将angularjs routeParams数据从两个不同的数据库表输入?
我的数据库有两个表,一个用于客户,另一个用于Tripsheets。 我想把客户的路线图数据称为TripSheet。 我试过这样的东西,但不管用 HTML JSJavascript 如何将angularjs routeParams数据从两个不同的数据库表输入?,javascript,angularjs,Javascript,Angularjs,我的数据库有两个表,一个用于客户,另一个用于Tripsheets。 我想把客户的路线图数据称为TripSheet。 我试过这样的东西,但不管用 HTML JS }) 由于您在同一控制器TripsheetCtrl1中进行调用,您将共享相同的$scope…因此,首先通过调用$http.get填充$scope.customers和$scope.whichItem,然后在getTripsheetsd中使用这些值 我最初是按照你的方式做的,但我似乎也没有工作,如果我将客户和tripsheet放在单独的控制
}) 由于您在同一控制器TripsheetCtrl1中进行调用,您将共享相同的$scope…因此,首先通过调用$http.get填充$scope.customers和$scope.whichItem,然后在getTripsheetsd中使用这些值
我最初是按照你的方式做的,但我似乎也没有工作,如果我将客户和tripsheet放在单独的控制器中,我该如何继续?这段代码让我困惑……为什么$scope.tripsheet在$http.get的成功中没有分配?如果您希望在控制器之间共享信息..我认为使用服务将是您想要的。$http.get被转发到customers数据库表,因此单独派生。不确定“转发到customers DB表”是什么意思…我理解$http.get正在访问customer表。但是,在.success中发生的事情并不与该表相关联……因此您可以在其中执行您想要的操作,对吗?我还删除了$http.get并使用您的customerFactory来获取客户。这意味着您可以在相应的工厂中弹出url和urltd。
<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>
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;
}
});
});
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;
});
});