initial data loading in angularjs

前端 未结 4 1295
借酒劲吻你
借酒劲吻你 2021-02-05 04:19

When building a web app where every page depends on many data sources, what\'s the best way to fetch the initial bits of data? When I look at twitter, I see the tweets that are

相关标签:
4条回答
  • 2021-02-05 04:56

    One way to do it is to create a directive that handles the initialization before binding happens. For example:

    app.directive('initdata', function() {
        return {
            restrict: 'A',
            link: function($scope, element, attrs) {
                if ( attrs.ngBind !== undefined)
                {
                    $scope[attrs.ngBind] = attrs.initdata ? attrs.initdata : element.text();
                }
            }
        };
    });
    

    This directive takes either the attribute value as initial value for the bound $scope property, or the textvalue of the element.

    Example usage:

    <div initdata="Foo Bar" ng-bind="test1"></div>
    <div initdata ng-bind="test2">Lorem Ipsem</div>
    

    Working example on http://jsfiddle.net/6PNG8/

    There's numerous way to elaborate on this; for example parsing the initdata as json and merging it with the scope, and making it work for more complicated binds, like $root.someprop. But the basis is remarkably simple.

    0 讨论(0)
  • 2021-02-05 04:57

    According to the answers on this question, a JSON object in a script tag on the page seems to be the way to go. If ayone comes up with a better idea, I'll accept your answer.

    0 讨论(0)
  • 2021-02-05 05:14

    You'll be referencing your controller anyway on page load, so you won't have to have an inline script tag.

    You can either set a default model and use the attribute ng-bind on initial load, or call a function to pass back data.

    It's pretty typical to fetch data on load in angularjs.

    0 讨论(0)
  • 2021-02-05 05:20

    Would it be best to couple Angularjs with an HTTP Client in the backend like Zend_Http_Client or Guzzle to let the server fetch the data. Then, pass the data as json to javascript upon render.

    I know Angularjs is designed for Single Page applications. That's why it makes sense that it lazy loads the data.

    However, if we're going to move to the approach where we still render the page dynamically and still delegate the task of organizing the content to Angularjs. What framework will be suitable to contain the AngularJS views. Right now, project templates like angular-seed are all static..

    That is, the idea is the server serves a page with the embedded json object. Then angular, takes over in the client side, Fetching additional content where needed.

    So instead of just a single page of contact (e.g: index.html), we would have several pages like profiles.html, products.html. The help of the backend would be particularly helpful say you have a section which doesn't change often like your username on the top right side of the page. For me, I just think it's better to have these data preloaded in your page and not have to ask the server after the page has been loaded.

    As bigblind have noticed, this seems to be the way sites like facebook, gmail, twitter does it. They contain the data embedded on page load. Then, load additional content via services afterwards.

    The idea is something like below:

    Webservice <---------- Backend------------> Frontend
         <------------------------------------------
    

    Backend delegates the task of querying the webservice to provide initial data in the rendered page to the client. Then client, can directly connect to webservice to fetch additional content.

    Using the above setup.. What is the ideal development stack?

    0 讨论(0)
提交回复
热议问题