简体   繁体   English

AngularJS-无法从服务获取数据

[英]AngularJS - Unable to get data from service

In an attempt to resolve a problem synchronizing between controllers. 为了解决控制器之间的同步问题。 I was trying to follow the solution provided here: 我正在尝试遵循此处提供的解决方案:

https://www.codementor.io/justinobney/keeping-angular-service-list-data-in-sync-with-controllers-a2hlwgwva https://www.codementor.io/justinobney/keeping-angular-service-list-data-in-sync-with-controllers-a2hlwgwva

For some reason with my setup this doesn't seem to work for me. 出于某种原因,我的设置似乎不适用于我。 My controllers are returning either empty or its just simply not working. 我的控制器返回的不是空值,就是根本不起作用。 Would anyone know why this approach wouldn't work? 有谁知道为什么这种方法行不通?

Main controller: 主控制器:

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 [];

});

Service: 服务:

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;
        }

    }
});

Why you need a copy? 为什么需要副本? just return the data, 只是返回数据,

 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;
            });
}

and then, 接着,

$scope.Current.incidents = Service.get_data();

I would suggest you to use custom promise. 我建议您使用自定义的Promise。 In the controller instead of returning the method directly to the property ,set the call back value to the property. 在控制器中,不要将方法直接返回到属性,而是将回调值设置为属性。

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;
     });

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM