我的代码有问题。
这是我的工厂代码:
.factory('shareDataService', function() {
var sharedData = {};
sharedData.shareData = function(dateFrom, dateTo) {
var from = dateFrom;
var to = dateTo;
alert(from + to);
};
return sharedData;
})
这是我的控制者:
.controller('getFormDataCtrl', ['$rootScope','$scope', '$http', 'shareDataService', function ($rootScope,$scope, $http, shareDataService) {
$scope.getFromBase = function () {
shareDataService.shareData($scope.dateFrom, $scope.dateTo)
}
$scope.bookingFormSubmit = function (){
var array = {
"from": ''// i want put dateFrom HERE,
"to": '',// i want put dateTo HERE
"yacht": $rootScope.yacht_id,
"customer": {
"fistname": $scope.firstname,
"lastname": $scope.lastname,
"birthday": $scope.birthday,
"phone": $scope.phone,
"email": $scope.email,
"country": $scope.country,
"city": $scope.city
}
}
}}])
此页面基于symfony2构建。我们使用sumfony2表单。 在第一个表单上,客户选择日期。有一个按钮“getFromBase()”
我想将这些数据保存在某个地方以便以后使用。
另一种形式是另一种形式。 有简单的客户文本输入(名称,姓氏等) 点击“bookingFormSubmit()”,我从这个表格中获取全部数据。 我需要从我的工厂添加这些数据(这两个变量)并在Json中创建一个对象。
答案 0 :(得分:0)
该服务中的实际数据有点过于私密。您需要稍微扩展范围并提供访问者。
<强>服务强>
.factory('shareDataService', function() {
var sharedData = {};
var from, to;
sharedData.getSharedData = function ()
{
return {from: from, to: to};
};
sharedData.shareData = function(dateFrom, dateTo) {
from = dateFrom;
to = dateTo;
};
return sharedData;
})
<强>用法强>
.controller('getFormDataCtrl', ['$rootScope','$scope', '$http', 'shareDataService', function ($rootScope,$scope, $http, shareDataService) {
$scope.getFromBase = function () {
shareDataService.shareData($scope.dateFrom, $scope.dateTo)
}
//NEW
var data = shareDataService.getSharedData;
$scope.bookingFormSubmit = function (){
var array = {
"from": data.from, //NEW
"to": data.to, //NEW
"yacht": $rootScope.yacht_id,
"customer": {
"fistname": $scope.firstname,
"lastname": $scope.lastname,
"birthday": $scope.birthday,
"phone": $scope.phone,
"email": $scope.email,
"country": $scope.country,
"city": $scope.city
}
}
}}])