尝试解决控制器之间的同步问题。我试图遵循这里提供的解决方案:
出于某种原因,我的设置似乎对我不起作用。我的控制器返回空或只是根本不工作。有谁知道为什么这种方法不起作用?
主控制器:
GDI_App.controller('Form_Controller', function ($scope, Service) {
Service.get_data();
//What I tried so far:
$scope.Current.incidents = Service.Current.data; //returns nothing.
$scope.Current.incidents = Service.current_data(); //returns [];
});
服务:
GDI_App.factory('Service', function($q) {
var Current ={}
Current.Data = [];
return{
get_data: function(){
var Fake_Data = [
{ "Data1": "123123", "Data2": "15437" },
{ "Data1": "432234", "Data2": "146" },
{ "Data1": "45654", "Data2": "3534" },
{ "Data1": "76587", "Data2": "78978" },
{ "Data1": "2342", "Data2": "5345878" },
{ "Data1": "178", "Data2": "34534" },
{ "Data1": "173838", "Data2": "354534" },
];
return $q.when(Fake_Data)
.then(function(data) {
angular.copy(data, Current.Data);
});
}
current_data: function(){
return Current.Data;
}
}
});
答案 0 :(得分:0)
为什么需要副本?只返回数据,
bool operator==(const Hex& lhs, const Hex& rhs)
{
return lhs.q == rhs.q && lhs.r == rhs.r;
}
然后,
get_data: function(){
var Fake_Data = [
{ "Data1": "123123", "Data2": "15437" },
{ "Data1": "432234", "Data2": "146" },
{ "Data1": "45654", "Data2": "3534" },
{ "Data1": "76587", "Data2": "78978" },
{ "Data1": "2342", "Data2": "5345878" },
{ "Data1": "178", "Data2": "34534" },
{ "Data1": "173838", "Data2": "354534" },
];
return $q.when(Fake_Data)
.then(function(data) {
angular.copy(data, Current.Data);
return data;
});
}
答案 1 :(得分:0)
我建议您使用自定义承诺。在控制器中,而不是将方法直接返回到属性,将回调值设置为属性。
get_data: function(){
var deferred=$q.defer();
var Fake_Data = [
{ "Data1": "123123", "Data2": "15437" },
{ "Data1": "432234", "Data2": "146" },
{ "Data1": "45654", "Data2": "3534" },
{ "Data1": "76587", "Data2": "78978" },
{ "Data1": "2342", "Data2": "5345878" },
{ "Data1": "178", "Data2": "34534" },
{ "Data1": "173838", "Data2": "354534" },
];
$q.when(Fake_Data)
.then(function(data) {
deferred.resolve(data)
});
return deferred.promise;
}
GDI_App.controller('Form_Controller', function ($scope, Service) {
Service.get_data().then(function(data){
$scope.Current.incidents=data;
});
});