我正在构建一个带有角度的SPA,我有两个控制器,并希望在其中共享数据。使用服务/工厂我无法共享数据。贝娄是代码。
/*this service is a helper to submit multipart form data*/
appDls.service('multipartForm', ['$http', function($http){
this.post = function(url,data){
var responseVar = "";
var fd = new FormData;
for(var key in data){
fd.append(key,data[key]);
}
return $http.post(url,fd,{
transformRequest: angular.indentity,
headers: {'Content-Type':undefined}
});
}
}]);
/*this factory is for sharing data across controllers*/
appDls.factory('sharedFactory', function(){
var dataTobeShared = {};
var interface = {};
interface.add = function(d){dataTobeShared = d;}
interface.put = function(){return dataTobeShared;}
return interface;
});
/*this controller is for the main portal user redirection and portal rendering*/
appDls.controller('DlsappController', ['$scope', '$state','multipartForm','sharedFactory', function($scope,$state,multipartForm,sharedFactory){
$scope.login = function(){
var url = "../scripts/routes.php/authen";
multipartForm.post(url,$scope.login).then(function(d){
$scope.data = d.data;
});
$scope.data = sharedFactory.add;
$scope.data = {};
}
}]);
/*this controller is for landing page routing*/
appDls.controller('landingController', ['$scope', '$state','multipartForm','sharedFactory', function($scope,$state,multipartForm,sharedFactory){
$scope.fucking = sharedFactory.put;
}]);
请告诉我上述代码中的错误...我正在寻找早期回复。
答案 0 :(得分:0)
我认为$scope.data = sharedFactory.add;
不会添加数据
所以使用
sharedFactory.add($scope.data)
如果你能分享一个小提琴,那就太棒了
答案 1 :(得分:0)
使用需要更正控制器中的调用功能
sharedFactory.add($scope.data);
sharedFactory.put();
var appDls = angular.module('myCtrl', []);
/*this service is a helper to submit multipart form data*/
appDls.service('multipartForm', ['$http',
function($http) {
this.post = function(url, data) {
var responseVar = "";
var fd = new FormData;
for (var key in data) {
fd.append(key, data[key]);
}
return $http.post(url, fd, {
transformRequest: angular.indentity,
headers: {
'Content-Type': undefined
}
});
}
}
]);
/*this factory is for sharing data across controllers*/
appDls.factory('sharedFactory', function() {
var dataTobeShared = {};
var interface = {};
interface.add = function(d) {
dataTobeShared = d;
}
interface.put = function() {
return dataTobeShared;
}
return interface;
});
/*this controller is for the main portal user redirection and portal rendering*/
appDls.controller('DlsappController', ['$scope', 'multipartForm', 'sharedFactory',
function($scope, multipartForm, sharedFactory) {
//$scope.login = function(){
// var url = "../scripts/routes.php/authen";
// multipartForm.post(url,$scope.login).then(function(d){
$scope.data = {
"Name": "No One"
};
// });
sharedFactory.add($scope.data);
//}
}
]);
/*this controller is for landing page routing*/
appDls.controller('landingController', ['$scope', 'multipartForm', 'sharedFactory',
function($scope, multipartForm, sharedFactory) {
$scope.fucking = sharedFactory.put();
}
]);

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myCtrl">
<div ng-controller="DlsappController">
DATA(DlsappController):{{data}}
</div>
<div ng-controller="landingController">
DATA(landingController):{{fucking}}
</div>
</div>
&#13;
希望这有帮助!!!