• Blogs (9)
    • 📱 236 - 992 - 3846

      📧 jxjwilliam@gmail.com

    • Version: ‍🚀 1.1.0
  • Angular resolve

    Blogs20142014-12-23


    Angular resolve

    In my blog, Angular promise resolve example is really helpful. MyCrl.resolve is pretty cool. Here I list an optional way to do resolve/promise by using $http

    //1. in ui-router:
    controller: 'ctrl',
    resolve: {
      friends: ['$http', function($http) {
        return $http.get('/api/friends.json').then(function(data) {
          return response.data;
        });
      }]
    }
    
    //2. in controller:
    .controller('ctrl', ['$scope', 'friends', function($scope, friends) {
      $scope.friends = friend;
    }]);
    
    //3. or using wrapping Friends factory instead of $http:
    controller: 'ctrl',
    resolve: {
      friends: ['Friends', function(Friends) {
        return Friends.get();
      }]
    }
    
    //4. keys: when using $http + resolve, using then() instead of success() in factory:
    .factory('Friends', function($http) {
     return {
      get: function($http) {
        return $http.get('api/friends.json').then(function(response) {
          return response.data;
        });
      }
     }
    }
    //5. using resolve object in router can resolve dependencies
    BEFORE controller instantiates.