[英]angular js to load initial data in context
where should i load basic data in angularjs app. 我应该在哪里在angularjs应用程序中加载基本数据。 i have created factory initialy i want to load data into service so that whenever i will require data i will use my service instead of hitting back to server.
我已经创建了工厂初始名称,我想将数据加载到服务中,以便每当我需要数据时,我都将使用我的服务而不是回服务器。
Hint:: My problem is i am applying ng-controller to different div's so many instance are created i dont want to load data from server with each instance. 提示:我的问题是我正在将ng-controller应用于不同的div,因此创建了许多实例,我不想为每个实例从服务器加载数据。
app.factory('feedbackFactory', function ($http) {
//factory code
});
app.factory('feedbackService', function ($http) {
//service code
});
app.controller('feedbackController', function ($scope, feedbackService,feedbackFactory $filter) {
// Constructor for this controller
init();
function init() {
feedbackFactory.get(1234, 1, 20).then(function(data)
{
$scope.feedbackItems=data;
// here load data into service
});
}
});
If your server supports caching $http
will default to use the E-Tags and only actually get the data back from the server once (assuming it doesn't update and the e-tag changes). 如果您的服务器支持缓存,则
$http
将默认使用E标签,并且实际上仅从服务器取回一次数据(假设它没有更新并且e标签发生了变化)。 This will cause it to hit the server however but get a 304
response. 这将导致它命中服务器,但是得到
304
响应。
If you want to force your application to only ever talk to the server once you would need to setup some kind of variable that gets checked to see if it should hit the server or not... 如果您想强制应用程序只与服务器对话一次,则需要设置某种变量,该变量将被检查以查看它是否应该到达服务器...
app.factory('feedbackFactory', function ($http) {
var checkServer = true;
var results = [];
function getStuff(x, y, z) {
if (checkServer) {
checkServer = false; // we don't want to check again! Maybe have a refresh button that sets this back to true?
// do http call...
$http.get("path").then(function(response) {
// Here is the magic... use angular.copy to keep your results object the same but update its values!
angular.copy(response, results);
});
}
return results; // this will be updated by our inital call to the server!
});
});
Thats probably not all valid code since I'm just typing off the top of my head, but it should get you close enough. 那可能不是所有有效的代码,因为我只是在脑海中打了个打字,但是它应该使您足够接近。 Big ones are bind to the
results
object and use angular.copy(httpResponseData, results)
to fill the data, you might want to return a $q.when(results)
if you want to bind to the call itself in a promise instead of directly binding to the feedbackFactory.results
; 大的绑定到
results
对象,并使用angular.copy(httpResponseData, results)
填充数据,如果您想在promise中绑定到调用本身,则可能要返回$q.when(results)
直接绑定到feedbackFactory.results
;
app.factory('feedbackFactory', function ($http, $q) {
var checkServer = true;
var results = [];
function getStuff(x, y, z) {
if (checkServer) {
checkServer = false; // we don't want to check again! Maybe have a refresh button that sets this back to true?
// do http call...
return $http.get("path").then(function(response) {
// Here is the magic... use angular.copy to keep your results object the same but update its values!
angular.copy(response, results);
return results; // If you want to bind to the promise instead of .results directly
});
}
return $q.when(results); // If you want to bind to the promise instead of .results directly
});
});
You can use $http cache, so your data won't be fetched from server after the first time. 您可以使用$ http缓存,因此第一次后将不会从服务器获取数据。 See this answer to see how to use cache in $http.
请参阅此答案以了解如何在$ http中使用缓存。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.