Search code examples
javascriptangularjslinkedin-api

Accessing LinkedIn API from AngularJS


I am building an app with Angular.js accesing the LinkedIn API. What happens is that when I do a call to the API, the model does not get refreshed inmediately, but after I do another change on the screen. For example, I have binded the API call to a button, but I have to press it twice for the screen to get the data refreshed. This is my controller:

    angbootApp.controller('AppCtrl', function AppCtrl($scope, $http) {
        $scope.getCommitData = function() {
            IN.API.Profile("me").fields(
                    [ "id", "firstName", "lastName", "pictureUrl",
                            "publicProfileUrl" ]).result(function(result) {
                //set the model
                $scope.jsondata = result.values[0];
            }).error(function(err) {
                $scope.error = err;
            });
        };
    });

And this is my button and a link with the content:

<div>
  <a href="{{jsondata.publicProfileUrl}}">{{jsondata.firstName}}</a>
  <form ng-submit="getCommitData()">
    <input type="submit" value="Get Data">
  </form>
</div>

EDIT: Explanation on how I did it here


Solution

  • You need to call $scope.$apply when retrieving the results/error

    angbootApp.controller('AppCtrl', function AppCtrl($scope, $http) {
        $scope.getCommitData = function() {
            IN.API.Profile("me").fields(
                    [ "id", "firstName", "lastName", "pictureUrl",
                            "publicProfileUrl" ]).result(function(result) {
                //set the model
                $scope.$apply(function() {
                    $scope.jsondata = result.values[0];
                });
            }).error(function(err) {
                $scope.$apply(function() {
                    $scope.error = err;
                });
            });
        };
    });
    

    Anything outside angular world does not trigger automatic refresh of the views. See $apply