javascript - AngularJS with Facebook SDK -
this question has answer here:
- how return response asynchronous call? 21 answers
i have problem angularjs.
i want return values within facebook sdk functions not let me them in variables.
the "perfil_nombre" , "perfil_foto" variables returned "undefined" , wish send scope.
any chance? i'm totally lost.
excuse bad english.
login.controller('inicio_ctrl', function($scope, $http, $timeout, $window) { var laspaginas; var response; var perfil_foto; var perfil_nombre; var resp; $scope.fblogin = function() { fb.login(function(response) { if(response.authresponse) { fb.api('/me', function(response) { perfil_nombre = response.name; //<<<<<-------- fb.api('/me/picture?redirect=false', function(resp) { perfil_foto = resp.data.url; //<<<<<-------- }); }); } else { console.log('user cancelled login or did not authorize.'); } }, { scope: 'email, public_profile, manage_pages,publish_pages,read_insights,user_friends, publish_actions'}); $scope.perfil_foto = perfil_foto; //<<<<<-------- undefined $scope.perfil_nombre = perfil_nombre; //<<<<<-------- undefined });
angular designed mvc or mv* framework. such better separate logic service , inject service controller. way can prevent negative interactions between angular , other frameworks. can difficult anticipate how angular interact outside libraries.
the simplest way make function work using javascript , wrap in .factory or .service.
for example
(function(){ 'use strict'; login.factory('fbservice', fbservice); fbservice.$inject = [(/*what ever dependencies needed*/)]; function fbservice(/*what ever dependencies needed*/){ //build profile object (i assume perfil translates profile in english) var profileobject; //add javascript logic here //create getters obtain data function getfoto(){ return profileobject.foto; } //expose getter return {getfoto: getfoto} } })(); (function(){ 'use strict'; login.controller('inicio_ctrl', inicioctrl); inicioctrl.$inject = ["$scope", "$http", "$timeout", "$window", "fbservice"]; function inicioctrl($scope, $http, $timeout, $window, fbservice){ var ctrl = this; ctrl.login = function(){ ctrl.perfil_folo = fbservice.getfoto(); } } })(); if can javascript work outside of angular allow preserve work , integrate angular
Comments
Post a Comment