app.factory('dataPassingService', function() {
var savedData = {};
function set(data) {
savedData = data;
}
function get() {
return savedData;
}
return {
set: set,
get: get
}
});
控制器1
$scope.text = '9';
dataPassingService.set($scope.text);
控制器2
$scope.mes = dataPassingService.get();
alert("the scope is "+scope.mes);
我使用app.factory将$ scope.text从controller1传递到控制器2,它完美无缺。我想将更多的范围从controller1传递到controller2,例如我想通过$ scope.text =' 9'和scope.text1 =' 10'然后使用datapassingService.get()将其带到controller2。提前感谢
答案 0 :(得分:2)
您可以传入一个键来唯一标识每个不同的数据,例如:
app.factory('dataPassingService', function() {
var savedData = {};
function set(key, data) {
savedData[key] = data;
}
function get(key) {
return savedData[key];
}
return {
set: set,
get: get
}
});
用法:
控制器1
$scope.text = '9';
dataPassingService.set("myKey", $scope.text);
dataPassingService.set("message", "foo");
控制器2
$scope.mes = dataPassingService.get("myKey");
$scope.message = dataPassingService.get("message");
alert("the scope is " + $scope.mes); // the scope is 9