次のサービスを宣言しています。
app.factory('data', ['$http', function ($http) {
var dataRecords = {};
//Retrieve the entries from the data file and categorize them according
//to the information types located in the overview page
$http.get('data.json')
.success(function (records) {
//Fill the dataRecords variable
});
return {
get: function () {
return dataRecords;
},
setNew: function (newRecords) {
dataRecords = newRecords;
}
};
}]);
ご覧のとおり、 data.json$http
ファイルからデータをフェッチします。ここでの問題はget
、データ サービスのメソッドを呼び出すと、data.json が空でなくても空のレコードが返されることです。
私が理解しているように$http.get()
、データは少し遅れて返されるため、get を呼び出すと、まだ入力されていないため、空の値が返されます。
サービスを呼び出して、サービスからデータを取得するときにデータが確実に入力されるようにするにはどうすればよいですか?