问题描述
我一直试图设置从HTTP POST响应于控制器的服务对象,并从另一个控制器得到它。结果
我见过这样或网站更侧重于得到它从HTML输入到一个控制器和其他控制器所获得的价值。该教程
I been trying to set an service object from a http post response to a controller and getting it from another controller.
The tutorials I seen in SO or sites are focused more on getting it from HTML input to a controller and another controller getting the value.
我想避免使用rootscope,我是新来的角的js。
I would want to avoid using rootscope and I'm new to angular-js.
第一个控制器(把数据1&放大器;数据2中的对象)
First Controller(To Put Data1 & Data2 in an object)
.controller('GetItem',function($scope, $http, $filter, $ionicPopup, $stateParams, $cordovaSQLite, $cordovaDatePicker, dataFactory){
...
console.log(resp.data);
var data1= resp.data.data1;
var data2= resp.data.data2;
}
第二控制器(为了获取对象和检索数据1和数据2)
2nd Controller (To get the object and retrieve data1 and data2)
一些指针会深深AP preciated!
Some pointers would be deeply appreciated!
UPDATE1:(保存功能的作品,但获取函数返回空):
Update1: (Save Function Works but Get Function Returns EMPTY):
.service('StoreService',function(){
var data1=[]; //{} returns me an empty object
this.save=function(data1){
alert('DATA: '+ data1); //able to retrieve string
this.data1=data1;
};
this.getData1=function(){
alert('DATA1: '+ data1); //unable to get string
return data1;
};
})
这是第二控制器从服务检索:
This is the 2nd Controller which retrieve from service:
.controller('unlock', function($scope, $timeout, dataFactory, $stateParams,StoreService) {
function test(){
console.log(StoreService.getData1());
更新2:(工作)
托米斯拉夫的plunker:
推荐答案
@atinder答案是的路要走。下面是该服务的例子:
@atinder answer is way to go. Here is the example of the service:
app.service('StoreService',function(){
var data1={};
var data2={};
this.save=function(data1,data2){
this.data1=data1;
this.data2=data2;
};
this.getData1=function(){
return data1;
};
this.getData2=function(){
return data2;
};
});
然后在第一个控制器:
Then in first controller:
.controller('firstController',function(StoreService){
.....
StoreService.save(resp.data.data1,resp.data.data2);
});
在第二个控制器:
.controller('secondController',function(StoreService,$scope){
$scope.data1 = StoreService.getData1();
$scope.data2 = StoreService.getData2();
});
这篇关于角服务设置和读取控制器之间的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!