我是angularJS的新手,只想集成facebook,我从facebook集成页面复制了示例代码,但没有得到任何东西。我的代码如下->
<html>
<head>
<title>Angular JS Facebook Intergration</title>
<script src = "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script src = "//connect.facebook.net/en_US/sdk.js"></script>
</head>
<body ng-app='fbApp'>
<div ng-controller="fbController">
{{getMyLastName()}}
</div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId: 'XXXXXXX',
status: true,
cookie: true,
xfbml: true,
version: 'v2.4'
});
};
var fbApp = angular.module("fbApp", []);
fbApp.controller('fbController',function($scope){
$scope.getMyLastName = function() {
facebookService.getMyLastName()
.then(function(response) {
$scope.last_name = response.last_name;
}
);
};
});
fbApp.factory('facebookService', function($q) {
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;
}
}
});
</script>
</body>
</html>
output: {{getMyLastName()}}
,请帮帮我,(这个例子只是为了学习),提前谢谢。
如何注入facebookService????
您需要将其添加为控制器函数的参数:
fbApp.controller('fbController',function($scope, facebookService){
$scope.getMyLastName = function() {
facebookService.getMyLastName()
.then(function(response) {
$scope.last_name = response.last_name;
}
);
};
});