简体   繁体   English

angular js在上下文中加载初始数据

[英]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.

相关问题 Angular 2 初始加载缓慢 - Angular 2 slow initial load 有角度的初始数据加载,将指令推迟到初始化之后 - Angular initial data load, defer directives until after initialise Angular Material Table 不会在初始加载时填充外部数据 - Angular Material Table won't populate with external data on initial load 如何将骨干初始数据加载到另一个js文件 - how to load backbone initial data to another js file 在Backbone.js中加载初始数据的最佳方法是什么? - What's the best approach to load initial data in Backbone.js? 在初始视图调用中从Angular JS中获取Spring MVC中的数据 - Getting data from Spring MVC in Angular JS in the initial view call 如何在Angular.js中为我的Json数据设置初始过滤器? - How to set an initial filter for my Json data in Angular.js? 在初始加载时初始化Angular $ scope - Initialize Angular $scope on initial load Angular 2应用程序的初始加载速度较慢,原因是在angular-cli.json中添加了外部第三方js库 - Angular 2 app slow initial load because of added external third party js libs to angular-cli.json Tabulator.js 停止初始 ajax 数据加载和第一次过滤后加载数据 - Tabulator.js Stop initial ajax data load and load data after first filter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM