Search code examples
javascriptangularjslinkedin-api

How to save user info from LinkedIn API into Angularjs controller using a custom service


Im new to angular and I'm having trouble saving user info from LinkedIn API to the scope in my controller without passing the scope to my custom service. I assume that is not the angular way of programming.

//html

<script type="text/javascript" src="//platform.linkedin.com/in.js">
  api_key: *********
  onLoad: onLinkedInLoad
</script>

// linkedIn button

<script type="in/Login">
</script>

// app.js

angular.module("linkedinTestApp",[]);

function onLinkedInLoad(){
  eScope.$apply(function(){
    eScope.getLinkedInData();
  })
};

// main controller

var eScope;
angular.module("linkedinTestApp").
controller('mainCtrl',function($scope,linkedInService){
  eScope = $scope;

  $scope.getLinkedInData = function(){
    linkedInService.OnLinkedInFrameworkLoad($scope);
  }
})

//custom service

angular.module('linkedinTestApp')
.service('linkedInService', function() {
    var scope;
    this.OnLinkedInFrameworkLoad = function(s) {
      scope = s;
      IN.Event.on(IN, "auth", this.OnLinkedInAuth);
      console.log("Test1");
    }

    this.OnLinkedInAuth = function() {
      IN.API.Profile("me").result(function(result){
        console.log(result);
        var profile = {
            vnaam: result.values[0].firstName,
            anaam: result.values[0].lastName,
            foto: result.values[0].pictureUrl,
            headline: result.values[0].headline,
            id: result.values[0].id
        }
        console.log(profile);
        scope.profile = profile;
      });
      console.log("Test2");
    }
});

Solution

  • Tested code. Took me 20-30 minutes to get api key and when i tested someone posted answer, but my code was tested so a post this, similar, answer. Also this is not the most elegant way to get profile in the controller, but I wanted to change as little code as possible(for similary).

    angular.module("linkedinTestApp",[]);
    
    function onLinkedInLoad(){
      eScope.$apply(function(){
        eScope.getLinkedInData();
      })
    };
    
    // main controller
    
    var eScope;
    angular.module("linkedinTestApp").
    controller('mainCtrl',function($scope,linkedInService){
      eScope = $scope;
    
      $scope.getLinkedInData = function(){
        linkedInService.OnLinkedInFrameworkLoad().then(function(profile){
          console.log('response ', profile);
      });
      }
    })
    
    //custom service
    
    angular.module('linkedinTestApp')
    .service('linkedInService', function($q) {
        this.OnLinkedInFrameworkLoad = function() {
          var deferred = $q.defer();
    
          IN.Event.on(IN, "auth", function(){
            deferred.resolve(OnLinkedInAuth())
          });
          return deferred.promise;
        }
    
        function OnLinkedInAuth() {
          var deferred = $q.defer();
    
          IN.API.Profile("me").result(function(result){
            console.log(result);
            var profile = {
                vnaam: result.values[0].firstName,
                anaam: result.values[0].lastName,
                foto: result.values[0].pictureUrl,
                headline: result.values[0].headline,
                id: result.values[0].id
            }
            deferred.resolve(profile);
          });
          return deferred.promise;
        }
    });