search

Home  >  Q&A  >  body text

angular.js - How to assign $http to the initial value of factory during angular initialization

Because I think the factory is a singleton, I use the http result as the return value of the factory. How can I assign an initial value to the factory with $http during angular initialization?
angular.module('some',[]) .factory('factory',function($http){ var some = {}; $http.get('url') .success(function(resp){ some.data = resp; }) return some.data; })
Now it is written like this. I hope that when angular is initialized, it can take some values ​​​​from the server and continue to use

phpcn_u1582phpcn_u15822808 days ago780

reply all(4)I'll reply

  • 習慣沉默

    習慣沉默2017-05-15 16:56:00

    You must have discovered that the problem with your writing is that data acquisition is asynchronous. some.data刚开始还是undefined。但你的直觉是对的:通用的数据应当放在factoryserviceInside. Now to solve the asynchronous problem, there are three solutions:

    1. Convert to sync. This is the most direct solution. Render the required data directly in the template on the server side, using the factory从HTML中取数据,一般用input tag. Along this line of thinking, a better way is to set up the resources before starting the Angular App. You can refer to this article: http://harttle.github.io/2015/05/31/angular-scope-initialize.html#1

    2. Asynchronous callback. Use factory to return a callback function, written like this:

      javascriptxxx.factory('some', function($http){
          var some = {}
          function get(cb){       
              if(some.data) return cb(some.data);     
              $http.get('').success(function(d){
                  cb(some.data = d);
              });
          }
          return get;
      });
      ...
      some.get(function(data){
          console.log(data);
      });
      
    3. Use Promise/Deffered mode for asynchronous operation. As projects become more and more complex, Promise is the ultimate solution. Angular provides an implementation of this pattern, which will return a Promise instance. Used like this: $q,它是一个Service,$http.get

      javascriptxxx.factory('some', function($q, $http){
          return $http.get('');
      });    
      ...
      some.success(function(data){
          console.log(data);
      });
      

    4. reply
      0
  • 给我你的怀抱

    给我你的怀抱2017-05-15 16:56:00

    angular.run(fn)

    reply
    0
  • 某草草

    某草草2017-05-15 16:56:00

    angularjs $http does not support synchronous acquisition

    Want to initialize or output response.write from backend to frontend
    Or use jquery's ajax blocking to get the data and then initialize angular
    For routing, you can use resolve to inject controller

    http://stackoverflow.com/questions/16286605/initialize-angularjs-servi...

    reply
    0
  • PHPz

    PHPz2017-05-15 16:56:00

    There are thousands of initialization methods. The key is not whether the value you want is obtained synchronously or asynchronously, but where and when do you use it? Goal orientation determines what method you use for initialization. Of course using promises is recommended in most cases.

    reply
    0
  • Cancelreply