Getting the user's friend list
Now that we know how to make requests to the Facebook API and get it to update correctly in our views, let's now see how to get the list of friends of the logged-in user.
We will create our function named loadFriends
and call it within the controller
option of the myFriends
directive, as shown in the following code snippet:
controller: function($scope) { $scope.loadFriends = function() { FB.api('/me/friends', function(response) { $scope.$apply(function() { $scope.myFriends = response.data; console.log($scope.myFriends); }); }); }; }
As you can see, the $scope.loadFriends
function loads the FB.api
method, making a request to the me/friends
end point.
The response from the request is stored in the $scope.myFriends
scope object. Note that we have to manually wrap it within the $apply
function, because the FB.api
call is external to the angular-context.
We'll now need...