I have tried many solutions For below mentioned error but nothing works for me I have refereed "https://developers.facebook.com/docs/javascript/howto/angularjs" for my implementation.
"Error: FB is not defined"
I am sharing my code below for controller and service files.
socialMedia.js -Controller
'use strict';
app
.controller('SocialMediaController', [
'$rootScope', '$scope', '$location', '$cookieStore' , 'SocialMedia', 'ngToast','$window',
function ($rootScope, $scope, $location, $cookieStore, SocialMedia, ngToast,$window) {
$window.fbAsyncInit = function() {
FB.init({
appId: '************',
status: true,
cookie: true,
xfbml: true,
version: 'v2.4'
});
};
// Load the SDK asynchronously
(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
$scope.testAPI = function() {
SocialMedia.getMyLastName()
.then(function(response) {
$scope.last_name = response.last_name;
}
);
};
}
]);
socialMedia.js -Services
'use strict';
app
.factory('SocialMedia', [
'$http', '$cookieStore', '$rootScope', 'appApi', function ($http, $cookieStore, $rootScope, appApi) {
return {
getMyLastName: function() {
var deferred = $q.defer();
FB.api('/me', {
fields: 'last_name'
}, function(response) {
if (!response || response.error) {
deferred.reject('Error occured');
} else {
deferred.resolve(response);
}
});
return deferred.promise;
}
}
}
]);
Html file
<input type="button" ng-click="testAPI()" value="Save" class="btn btn-primary md-trigger" />
Let me konw where I am wrong in this.