AngularJS - loading data into a page loading service

In an angularjs application, I am trying to use a custom service to store the main data of the "items" application. However, I would like to load data into the service when the page loads, in order to avoid a separate ajax request to the server in order to receive it. What would be the cleanest way to do this?

Here is a snippet of my service for reference:

app.factory('items', function() {
    var itemsService = {},
        items = [];

    itemsService.list = function() {
        return items;
    };

    itemsService.add = function() {
        /* ... */
    };

    return itemsService;
});

And on the backend, I use node.js + expressjs + jade, so I will enter data into the page using:

!{JSON.stringify(items)}
+5
source share
2 answers

JavaScript. items ( items).

+3

- :

app.run(['items', function (items) {
   items.load();
}])

, items load ( - ), Ajax, $http $resource.

+1

All Articles